RegEx issue in PHP - 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.

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 preg_replace and newline characters [duplicate]

I use a regex pattern i preg_match php function. The pattern is let's say '/abc$/'. It matches both strings:
'abc'
and
'abc
'
The second one has the line break at its end. What would be the pattern that matches only this first string?
'abc'
The reason why /abc$/ matches both "abc\n" and "abc" is that $ matches the location at the end of the string, or (even without /m modifier) the position before the newline that is at the end of the string.
You need the following regex:
/abc\z/
where \z is the unambiguous very end of the string, or
/abc$/D
where the /D modifier will make $ behave the same way as \z. See PHP.NET:
The meaning of dollar can be changed so that it matches only at the very end of the string, by setting the PCRE_DOLLAR_ENDONLY option at compile or matching time.
See the regex demo

GUID validation RegEx fails new line character [duplicate]

I use a regex pattern i preg_match php function. The pattern is let's say '/abc$/'. It matches both strings:
'abc'
and
'abc
'
The second one has the line break at its end. What would be the pattern that matches only this first string?
'abc'
The reason why /abc$/ matches both "abc\n" and "abc" is that $ matches the location at the end of the string, or (even without /m modifier) the position before the newline that is at the end of the string.
You need the following regex:
/abc\z/
where \z is the unambiguous very end of the string, or
/abc$/D
where the /D modifier will make $ behave the same way as \z. See PHP.NET:
The meaning of dollar can be changed so that it matches only at the very end of the string, by setting the PCRE_DOLLAR_ENDONLY option at compile or matching time.
See the regex demo

greedy character matching at end of string

I'm trying to match the following string:
controller1/action1/something
With the following regex:
(?P<controller>[[:alnum:]]+)/(?P<action>[[:alnum:]]+)/(.*)
For some reason it doesn't find the last part of the string: something. But it works when i change the * to + at the end of the regex:
(?P<controller>[[:alnum:]]+)/(?P<action>[[:alnum:]]+)/(.+)
With that regex it does find the something string. But i want to use .* (or .*?) because i want this regex to succeed also when it doesn't have something at the end.
So it should also succeed when the string is: controller1/action1/
So why doesn't it work with (.*) or (.*?) but works with .+? The difference should simply be that the first says "zero or more characters" and the last "one or more". I simply want to check for "zero or more".
PS. I don't want to use ^ and $ to denote the beginning and end of the string due to a complexer problem. Simply stated, this pattern doesn't always occur for strings at the end.
So it should also succeed when the string is: controller1/action1/
I suspect since this input is part of some bigger string that's why .* isn't working for you. suggest you to post some real examples of your input text.
Meanwhile can you try this regex:
"#(?P<controller>[^/]+)/(?P<action>[^/]+)/([^/]*)#"
You just have to make the last group optional to make it match controller1/action1/
(?P<controller>[[:alnum:]]+)/(?P<action>[[:alnum:]]+)/(.+)?

pregmatch for numbers only

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

Categories