pregmatch for numbers only - php

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);

Related

How to pull a IP out of a string using preg_match in PHP:? [duplicate]

What is the difference between "\\w+#\\w+[.]\\w+" and "^\\w+#\\w+[.]\\w+$"? I have tried to google for it but no luck.
^ means "Match the start of the string" (more exactly, the position before the first character in the string, so it does not match an actual character).
$ means "Match the end of the string" (the position after the last character in the string).
Both are called anchors and ensure that the entire string is matched instead of just a substring.
So in your example, the first regex will report a match on email#address.com.uk, but the matched text will be email#address.com, probably not what you expected. The second regex will simply fail.
Be careful, as some regex implementations implicitly anchor the regex at the start/end of the string (for example Java's .matches(), if you're using that).
If the multiline option is set (using the (?m) flag, for example, or by doing Pattern.compile("^\\w+#\\w+[.]\\w+$", Pattern.MULTILINE)), then ^ and $ also match at the start and end of a line.
Try the Javadoc:
http://download.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html
^ and $ match the beginnings/endings of a line (without consuming them)

Php lookahead assertion at the end of the regex

I want to write a regex with assertions to extract the number 55 from string unknownstring/55.1, here is my regex
$str = 'unknownstring/55.1';
preg_match('/(?<=\/)\d+(?=\.1)$/', $str, $match);
so, basically I am trying to say give me the number that comes after slash, and is followed by a dot and number 1, and after that there are no characters. But it does not match the regex. I just tried to remove the $ sign from the end and it matched. But that condition is essential, as I need that to be the end of the string, because the unknownstring part can contain similar text, e.g. unknow/545.1nstring/55.1. Perhaps I can use preg_match_all, and take the last match, but I want understand why the first regex does not work, where is my mistake.
Thanks
Use anchor $ inside lookahead:
(?<=\/)\d+(?=\.1$)
RegEx Demo
You cannot use $ outside the positive lookahead because your number is NOT at the end of input and there is a \.1 following it.

Regular expression that match string format "1,22,3,44"

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);

RegEx issue in PHP

I have the following RegEx:
/[a-zA-Z\d ']{1,30}/
and the following string:
some text'&&
Now, that RegEx returns true on the string. I suppose that it matches the part without "&&". I'd like to ask how can I limit characters to alphanumeric characters, including space and apostrophes as the RegEx from above writes.
Thanks!
Anchor your regex:
/^[a-zA-Z\d ']{1,30}$/
^ means "start of string" and $ means "end of string" so adding those markers forces the regex to either match the entire string or not at all.

PHP find words which are not in regex

I have written following regular expression /^[A-Za-z0-9-_\s]*$/ in PHP which allows numbers, letters, spaces, hyphen and underscore. I want to display those matches which are not valid against the regex i.e "My Name is Blahblah!!!" should give me "!!!" output.
Use the caret symbol inside the character class to invert the match and remove the start (^) and end ($) characters:
/[^A-Za-z0-9-_\s]+/
http://php-regex.blogspot.com/2008/01/how-to-negate-character-class.html
If you replace all the matches with the empty string then you'll get the non-matching parts back:
preg_replace('/[A-Za-z0-9-_\s]+/', '', $string)
This will work for any arbitrary regex, but for your specific regex #Andy's solution is simpler.
Notice that I removed the anchors ^ and $ to make this work.
preg_replace("/^[A-Za-z0-9-_\s]*$/","","My Name is Blahblah!!!") // Output: "!!!"
Or, if you want all the groupings of them
preg_split("/^[A-Za-z0-9-_\s]*$/","","My Name is Blahblah!!!")
You have to put the hiphen - at the begining or at the end of the character class or escape it, so your regex would be :
/[^-A-Za-z0-9_\s]+/
or
/[^A-Za-z0-9_\s-]+/
or
/[^A-Za-z0-9\-_\s]+/

Categories