php regular expression for "|" and digits - php

I want know, what regular expression should I have for my string. My string can contains only "|" and digits.For example: "111|333|111|333". And string must begin from number. I am using this code, but he is ugly:
if (!preg_match('/\|d/', $ids)) {
$this->_redirect(ROOT_PATH . '/commission/payment/active');
}
Thank you in advance. Sorry for my english.

Looking at your example I assume you are looking for a regex to match string that begin and end with numbers and numbers are separated with |. If so you can use:
^\d+(?:\|\d+)*$
Explanation:
^ - Start anchor.
\d+ - One ore more digits, that is a number.
(? ) - Used for grouping.
\| - | is a regex meta char used for alternation,
to match a literal pipe, escape it.
* - Quantifier for zero or more.
$ - End anchor.

The regex is:
^\d[|\d]*$
^ - Start matching only from the beginning of the string
\d - Match a digit
[] - Define a class of possible matches. Match any of the following cases:
| - (inside a character class) Match the '|' character
\d - Match a digit
$ - End matching only from the beginning of the string
Note: Escaping the | is not necessary in this situation.

A string that contains only | or digits and begins with a digit is written as ^\d(\||\d)*$. That means: either \| (notice the escape!) or a digit, written as \d, multiple times.
The ^ and $ mean: from start to end, i.e. there’s no other character before or after that.

I think /^\d[\d\|]*$/ would work, however, if you always have three digits separated by bars, you need /^\d{3}(?:\|\d{3})*$/.
EDIT:
Finally, if you always have sequences of one or more number separated by bars, this will do: /^\d+(?:\|\d+)*$/.

Related

How do I test if string maches integer:integer with preg_match?

I need a regular expression to test if string matches integer:integer (ex: 9:4).
I have tried
preg_match("[0-9]:[0-9]", $str)
but it's not correct.
You have to mark the start and end of the regular expression, usually with /.
Try this:
preg_match("/[0-9]:[0-9]/", $str)
One hint: you can use \d instead of [0-9].
If you want to make sure that the string only contains digit:digit, use ^ as the marker for the start of the string and $ for the end:
preg_match("/^[0-9]:[0-9]$/", $str)
Also, add + to match numbers of more than one digit:
preg_match("/^[0-9]+:[0-9]+$/", $str)
^[0-9](:[0-9])*$
^ matches the start of the string, and $ matches the end, ensuring that you're examining the entire string. It will match a single digit, plus zero or more instances of a colons followed by a digit after it.

PHP Pattern Validation

I'm having a bit of trouble getting my pattern to validate the string entry correctly. The PHP portion of this assignment is working correctly, so I won't include that here as to make this easier to read. Can someone tell me why this pattern isn't matching what I'm trying to do?
This pattern has these validation requirements:
Should first have 3-6 lowercase letters
This is immediately followed by either a hyphen or a space
Followed by 1-3 digits
$codecheck = '/^([[:lower:]]{3,6}-)|([[:lower:]]{3,6} ?)\d{1,3}$/';
Currently this catches most of the requirements, but it only seems to validate the minimum character requirements - and doesn't return false when more than 6 or 3 characters (respectively) are entered.
Thanks in advance for any assistance!
The problem here lies in how you group the alternatives. Right now, the regex matches a string that
^([[:lower:]]{3,6}-) - starts with 3-6 lowercase letters followed with a hyphen
| - or
([[:lower:]]{3,6} ?)\d{1,3}$ - ends with 3-6 lowercase letters followed with an optional space and followed with 1-3 digits.
In fact, you can get rid of the alternation altogether:
$codecheck = '/^\p{Ll}{3,6}[- ]\d{1,3}$/';
See the regex demo
Explanation:
^ - start of string
\p{Ll}{3,6} - 3-6 lowercase letters
[- ] - a positive character class matching one character, either a hyphen or a space
\d{1,3} - 1-3 digits
$ - end of string
You need to delimit the scope of the | operator in the middle of your regex.
As it is now:
the right-side argument of that OR runs up until the very end of your regex, even including the $. So the digits, nor the end-of-string condition do not apply for the left side of the |.
the left-side argument of the OR starts with ^, and only applies to the left side.
That is why you get a match when you supply 7 lowercase characters. The first character is ignored, and the rest matches with the right-side of the regex pattern.

PHP: Complex Multi-Line RegEx, Tilde delimeters

I need to generate a regex that will match the following format:
-1 LKSJDF LSAALSKJ~
Syjsdf
lkjdf
This block may contain multiple characters including digits, colons, etc. Any character other than a tilde.
~
I'm currently using this:
/(-\d|\d)\s([^$\~][a-zA-Z\s]*)\~\n/s
Which matches the first line fine. I need to capture the -1 through 60 that begins the pattern, the words after the space and up until the first tilde. I then need to capture all of the text BETWEEN the tildes.
I'm not the strongest with regex in the first place, but I'm having trouble getting this to work without also capturing the tildes.
You can use
'/^(-?\d+)\s+([^~]*)~([^~]+)~/m'
See demo
The regex matches:
^ - start of a line (due to /m modifier ^ does not match start of string any longer)
(-?\d+) - (Group 1) a one or zero - followed with one or more digits
\s+ - one or more whitespace symbols (to only match tab and regular spaces, use \h+ instead)
([^~]*) - (Group 2) zero or more characters other than a ~ (you can force to match these characters on the first line only by adding a \n\r to the negated character class - [^~\n\r])
~ - a literal leading tilde
([^~]+) - (Group 3) one or more characters other than a tilde
~ - a literal trailing tilde
If you need to only match these strings if the number is an integer between -1 and 60, you can use
'/^(-1|[1-5]?[0-9]|60)\s+([^~]*)~([^~]+)~/m'
See another demo
Here, the first group matches integer numbers from -1 to 60 with (-1|[1-5]?[0-9]|60) alternation group. -1 and 60 match literal numbers, and [1-5]?[0-9] matches one or zero (optional) digit from 1 to 5 (replace with [0-5]? if a leading zero is allowed) and then any one digit may follow.

Regex to match numbers, # # % signs

I am trying to write a regex that matches all numbers (0-9) and # # % signs.
I have tried ^[0-9#%#]$ , it doesn't work.
I want it to match, for example: 1234345, 2323, 1, 3#, %#, 9, 23743, #####, or whatever...
There must be something missing?
Thank you
You're almost right... All you're missing is something to tell the regular expression there may be more than once of those characters like a * (0 or more) or a + (1 or more).
^[0-9#%#]+$
The ^ and $ are used do indicate the start and end of a string, respectively. Make sure that you string only contains those characters otherwise, it won't work (e.g. "The number is 89#1" wouldn't work because the string begins with something other than 0-9, #, %, or #).
Your pattern ^[0-9#%#]$ only matches strings that are one character long. The [] construct matches a single character, and the ^ and $ anchors mean that nothing can come before or after the character matched by the [].
If you just want to know if the string has one of those characters in it, then [0-9#%#] will do that. If you want to match a string that must have at least one character in it, then use ^[0-9#%#]+$. The "+" means to match one or more of the preceding item. If you also want to match empty strings, then use [0-9#%#]*. The "*" means to match zero or more of the preceding item.
It should be /^[0-9#%#]+$/. The + is a qualifier that means "one or more of the preceding".
The problem with your current regex is that it will only match one character that could either be a number or #, %, or #. This is because the ^ and $ characters match the beginning and the end of the line respectively. By adding the + qualifier, you are saying that you want to match one or more of the preceding character-class, and that the entire line consists of one or more of the characters in the specified character-class.
remove the caret (^), it is used to match from the start of the string.
You forgot "+"
^[0-9#%#]+$ must work

What does this regular expression mean? /^obj(\d+)\-{0,1}(|mi\d{0,1}|critical|questionText|answerText\-{0,1}\d+)$/ [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 6 years ago.
What does this regular expression mean?
preg_match("/^obj(\d+)\-{0,1}(|mi\d{0,1}|critical|questionText|answerText\-{0,1}\d+)$/", $k, $a)
the preg_match is a php function that should translate it
This expression, commented would look like
^ // start of line
obj // literal obj
(\d+) // one or more digits (0-9), captured in a group
-{0,1} // optional dash
( // start second capturing group
// nothing
| // ... OR ...
mi\d{0,1} // literal mi, followed by an optional digit
| // ... OR ...
critical // literal critical
| // ... OR ...
questionText // literal questionText
| // ... OR ...
answerText // literal answerText
-{0,1} // optional dash
\d+ // one or more digits (0-9)
) // end of capturing group
$ // end of line
An example of what is matches would be
obj1000-critical or obj1000answerText-100
^obj(\d+)-{0,1} means that the string is the start of a line and begins with obj, followed by a number of at least 1 digit, then there might be a - sign.
(|mi\d{0,1}|critical|questionText|answerText-{0,1}\d+) means that the text is one of the following:
nothing
mi which might be followed by a digit
critical
questionText
answerText which can be followed by one - sign, and after that a number of at least 1 digit
Then there's the end of the line. The search is case sensitive.
I'd firstly suggest finding a regex tutorial and reading up on how they are structured. This isn't a particularly complciated regex so you should be able to figure it out with some bookwork and it will mean you won't have to ask abotu the next regex you come across. ;-)
What the regex means broken down is as follows:
^ - matches the beginning of the string.
obj - this is literal text so will match those characters at the beginning of the string
(\d+) - this will match one or more digits (0-9) and the brackets will mean they are captured in such a way as they could be used after the parsing.
-{0,1} - this will match 0 or 1 "-" characters.
(|mi\d{0,1}|critical|questionText|answerText-{0,1}\d+) - again the brackets will capture this as a group. The "|" is used as an "or" so it will match any of the separated values. Although I'm not sure I think that the fact it starts with a "|" might mean it will match an empty string.
mi\d{0,1} - matches the miteral string mi followed by 0 or 1 digits.
critical, questionText - these are both literal options that match the exact text
answerText-{0,1}\d+ - this will match the literal string answerText followed by an optional "-" and one or more digits.
$ - the string must end immediately after the previous match.
I hope that makes sense to you. As I say, check some tutorials and docs if you need more help. :)
IT will match a string that starts with obj, followed by a numer, an optional dash, followed by either
nothing
mi followd by a number
critical
questionTest
answerText followed by an optional - which is in turn followed by a number (not optional)
and then the end of the string.
Examples:
obj932-mi21
obj3124critical
obj1-answerText-86
obj654answerText8
obj23-
It matches a certain format for lines that contain, for example:
obj1-mi5
obj2critical
obj3-questionText
obj4answerText-0
Do these patterns look familiar?
preg_match will find that pattern in $k and store matches in $a. Do the following to see what it found.
print_r($a);

Categories