OK this regex will match string like 2aa, a2, 2aaaaaa, aaaa2, aaa2aaaa, 2222a2222-2222-aaaa... in short, mix of alphanumeric characters in a sequence:
preg_match("/(?:\d+[a-z]|[a-z]+\d)[a-z\d]*/i")
now I want to exclude something but I'm stuck, something like this doesn't work
preg_match("/(?!1920x1200|1920x1080)(?:\d+[a-z]|[a-z]+\d)[a-z\d]*/i")
for example the string aaaaa222aaa1920x1200bbbbb1234556789 is still matched but it shouldn't because it contains 1920x1200
any help is appreciated :)
i'm using regex found here for matching alphanum sequences Regex: match only letters WITH numbers
regex test: https://regex101.com/r/vU9aU9/1
Your negative lookahead should have .* in front to allow for 0 or more characters before not-allowed text. Also use anchors in your regex.
regex should be:
preg_match('/^.*?1920x1200.*$(*SKIP)(*F)|(?:\d+[a-z]|[a-z]+\d)[a-z\d]*/im')
RegEx Demo
Related
Can you help me out with this one? I have a list of words like this:
sachbearbeiter/-in
referent/-in
anlagenführer/-in
it-projektleiter/-in
I want to select only:
sachbearbeiter/-in
referent/-in
This is my current regex: ([a-z]+)/-(in)
The problem is it hits all even the ones with - and with ü
Thank you in advance.
You can use anchors to match the word you want:
^([a-z]+)/-(in)$
^---- Here ----^
Working demo
Update: for your comment, if you want to accept aumlats you can use unicode flag with \w like this:
^(\w+)/-(in)$
Working demo
You need to specify beginning & end of string so that it can match exact chars
change your regex to
^([a-z]+)/-(in)$
^ -> stands for beginning of string
$-> for end of string
Your current regex i.e. ([a-z]+)/-(in) does escape the / character and also trying to look into substrings that matches the pattern, so it'll show each of them.
Regex should be : ^([a-z]+)\/-(in) i.e. it should start with only small case alphabets with escaped /
Can someone tell me why this does not work ? - https://regex101.com/r/hJ5zN6/11
Test string:
[test][dzspgb_container][dzspgb_row][dzspgb_row_part part="1.4"][dzspgb_element text="whwaha" type_element="text"][/dzspgb_element][dzspgb_element text="test" type_element="text"][/dzspgb_element][/dzspgb_row_part][dzspgb_row_part part="1.4"][/dzspgb_row_part][dzspgb_row_part part="1.4"][/dzspgb_row_part][dzspgb_row_part part="1.4"][/dzspgb_row_part][/dzspgb_row][dzspgb_container]test second[/dzspgb_container][/dzspgb_container][/thisbreaks]
Test regex:
*\[dzspgb_container(.*?)](.*?)\[\/dzspgb_container\](?!\s*\[\/)*
If we remove [/thisbreaks] from the string, it will work.
It's because of the negative lookahead assertion at the end. I suggest you to remove that lookahead and use a greedy regex pattern like below.
\[dzspgb_container(.*?)](.*)\[\/dzspgb_container\]
DEMO
(?!\s*\[\/) asserts that the match won't be followed by (zero or more space characters and a [ symbol)
I'm trying to do a regex that would accept the following:
/profile
/profile/photo
/profile/video
but would not accept anything else than /photo and /video:
I tried the following but without any luck:
/profile((\/(photo|video))|[^\w])/
Can someone tell me what's wrong with my regex.
Thanks
this'll work:
^\/profile(?:\/(photo|video))?$
about your regex: /profile((\/(photo|video))|[^\w])/
\w matches a-z, A-Z, 0-9, _ so ^\w will match any other character. for eg. profile/, profile%, profile# will result in a match. since you've written [^\w] and not [^\w]+ or * therefore it'll match a single non-word character after profile*here*
you've not prepended profile with a /
derived from what you've written, this'll work too:
^\/profile(\/(photo|video))?$
Try this:
/^\/profile(\/photo|\/video|)?$/
You can use this regex:
^\/profile(\/photo|\/video)?$
Here you have a working example:
http://regex101.com/r/iE2yJ4/1
I'm using this regular expression to test if a username is valid:
[A-Za-z0-9 _]{3,12} when I test it for matches in a text editor with the string test'ing, it highlights 'test' and 'ing', but when I use the following code in PHP:
if(!preg_match('/[A-Za-z0-9 _]{3,12}/', $content) where $content is test'ing and it should return FALSE, it still returns true.
Is there something wrong with my regular expression? I need:
Minimum length 3, max 12 {3,12}
No spaces/underscores in front or after the string, and no spaces/underscores in a row anywhere
(I'm using additional checks for this because I'm not very good with regex)
Only alphanumerics, spaces and underscores allowed [A-Za-z0-9 _]
Thanks in advance...
You're missing the anchors in the regular expression, so the regex can comfortably match 3 characters in the character class anywhere in the string. This is not what you want. You want to check if your regex matches against the entire string. For that, you need to include the anchors (^ and $).
if(!preg_match('/^[A-Za-z0-9 _]{3,12}$/', $content)
^ ^
^ asserts the position at the beginning of the string and $ asserts position at the end of the string. It's important to note that these meta characters do not actually consume characters. They're zero-width assertions.
Further reading:
Regex Anchors on regular-expressions.info
The Stack Overflow Regular Expressions FAQ
You should add start and end anchors (^$):
if(!preg_match('/^[A-Za-z0-9 _]{3,12}$/', $content)
The anchor ^ matches the start of the string and $ matches the end. That way, it will only match if the whole string satisfies your regex.
Hope that helps
get_by_my_column
If I only want to match the get_by portion of the above string, how can I do this? I keep reading on this regex cheatsheet that I should use \n but I can't figure out how to implement it properly...
I've tried variations of the following...
/((_){2})/
/(_+){2}/
/(\w+?_\w+?)_\w+/ (use non greedy quantifiers, your substring should be in capture group 1)
or just /\w+?_\w+?/ <---(edit: won't work, you do need that second underscore as regex structure to force the non greedy \w up to it :])
Do you need to use a regex for this? You could use explode() and just grab the first two elements of the resulting array.
Try
preg_match('/(^[a-z]+[_][a-z]+)/', $string, $results);
This matches a string that starts with a group of letters followed by an underscore followed by another set of letters.
Edit: (lowercase letters)
try /^get_by. ^ for the condition that g must be the starting character.