I tried:
preg_match("/((\d)+,?)+/",$string)
it returns correct result for:
1,2,3,4,5
but it also matches with:
1,2a
1aaaa
1,2,3,
How can I fix this?
You could do
^\d+(,\d+)*$
The first ^\d+ matches for the beginning of the string and a number. Then you optionally match for a comma followed by a number (and that groups can be present 0 or multiples times). Finally the $ match for the end of string. To make sure there are nothing left after the last number.
Here is a state machine showing how the expression works :
Edit live on Debuggex
This can match for example : 8, 1,2,4.
But wont match : 8,, ,2 or 1,2a
You can use the anchors ^ and $ to prevent other matches:
preg_match("/^((\d)+,?)+$/",$string)
^ ^
Although... your expression might be better like this:
preg_match("/^\d+(,\d+)*$/",$string)
What is happening with ((\d)+,?)+ is that it matches the first few characters it finds and once it is done, it says it matched.
The anchors ^ and $ mean the beginning of the string and the end of the string respectively, meaning that the beginning must match until the end.
try this:
preg_match('~^(?:\d+(?:,(?!$)|$))+$~', $string);
Related
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.
I am trying to use preg_match to extract numbers only. I have tried the fllowing code but it does not work. Any ideas how to only get the number?
preg_match_all('/^[0-9]+$/i', '<userinfo>Jsome text here 16586 more text here.</userinfo>', $result);
Here's the regular expression you should be using:
/<userinfo>.*?(\d+).*?<\/userinfo>/
This will match
<userinfo>Jsome text here 16586 more text here.</userinfo>
and the 2nd element of the returned array (array[1]) will have the number you need. (16586)
Your regex shouldn't include the ^ and $ symbols, as they denote the start and end of the string, which doesn't exist in the middle of a string.
Take them out and the regex should work just fine.
The symbol ^ specify the beggening of the line and $ - line end. So your RegExp completely wrong. You should write
preg_match_all('/\d+/', '<userinfo>Jsome text here 16586 more text here.</userinfo>', $result);
Also you do not need to specify i modificator, that means searching with case ignore mode.
You have used ^ and $ in your regular expression. ^ means match the start of the string and $ means match the end of the string. As such, you've written a regex to match a string that contains only a number; i.e. your regex says start of string then number then end of string. Remove these characters and you'll be able to match a number.
Have you tried?
preg_match_all('[0-9]+', '<userinfo>Jsome text here 16586 more text here.</userinfo>', $result);
I have to create regex to match ugly abbreviations and numbers. These can be one of following "formats":
1) [any alphabet char length of 1 char][0-9]
2) [double][whitespace][2-3 length of any alphabet char]
I tried to match double:
preg_match("/^-?(?:\d+|\d*\.\d+)$/", $source, $matches);
But I coldn't get it to select following example: 1.1 AA My test title. What is wrong with my regex and how can I add those others to my regex too?
In your regex you say "start of string, followed by maybe a - followed by at least one digit or followed by 0 or more digits, followed by a dot and followed by at least one digit and followed by the end of string.
So you regex could match for example.. 4.5, -.1 etc. This is exactly what you tell it to do.
You test input string does not match since there are other characters present after the number 1.1 and even if it somehow magically matched your "double" matching regex is wrong.
For a double without scientific notation you usually use this regex :
[-+]?\b[0-9]+(\.[0-9]+)?\b
Now that we have this out of our way we need a whitespace \s and
[2-3 length of alphabet]
Now I have no idea what [2-3 length of alphabet] means but by combining the above you get a regex like this :
[-+]?\b[0-9]+(\.[0-9]+)?\b\s[2-3 length of alphabet]
You can also place anchors ^$ if you want the string to match entirely :
^[-+]?\b[0-9]+(\.[0-9]+)?\b\s[2-3 length of alphabet]$
Feel free to ask if you are stuck! :)
I see multiple issues with your regex:
You try to match the whole string (as a number) by the anchors: ^ at the beginning and $ at the end. If you don't want that, remove those.
The number group is non-catching. It will be checked for matches, but those won't be added to $matches. That's because of the ?: internal options you set in (?:...). Remove ?: to make that group catching.
You place the shorter digit-pattern before the longer one. If you swap the order, the regex engine will look for it first and on success prefer it over the shorter one.
Maybe this already solves your issue:
preg_match("/-?(\d*\.\d+|\d+)/", $source, $matches);
Demo
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
I'm trying to replace all the letters and spaces after the first two, using PHP's preg_replace. Here is my failed attempt at doing so:
echo preg_replace('/^[a-zA-Z]{2}([a-zA-Z ])*.*$/i','','FALL 2012'); //expected output is "FA2012", but it outputs nothing
I'm just trying to replace the part in parentheses ([a-zA-Z ]) .. I'm guessing I'm not doing it right to just replace that part..
You're asking it to replace the entire string. Use a lookbehind to match the first two characters instead.
echo preg_replace('/(?<=^[a-z]{2})[a-z ]*/i','','FALL 2012');
By the way, the i modifier means it's case insensitive, so you don't need to match both uppercase and lowercase characters.
The /i at the end makes it case-insensitive. The (?<=regex) means look immediately before the current position for the beginning of the line followed by 2 letters.
echo preg_replace('/(?<=^[a-z]{2})[a-z ]*/i','','FALL 2012');
you are saying to replace the entire match with blank (''). You want to put the parenthesis around the parts you want to keep and then replace with $1$2 which is equal to what is in the first ($1) and second ($2) set of parenthesis.
preg_replace("/^([a-z]{2})[a-z\s]*(.*)$/i", '$1$2', $string);
In this case you can get away with a (?<=lookbehind); however, in certain other cases you may find the \K escape to be more suitable. What it does is reset the start offset value passed by the application to the current position in the subject, effectively dumping the portion of the string that was consumed thus far in the current match. For example:
preg_replace('^[a-z]{2}\K[a-z ]*/i', '', 'FALL 2012')
Now only the substring matched by [a-z ]* is substituted.