How to match 2nd instance in regex - php

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.

Related

RegEx expression to hit only words with a-z and no aumlats

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 /

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.

in need of a php preg_replace regex

I am writing a script that needs to download images related to a product ID array to an external website.
Here are the possible product ID combinations.
ABC1234AB
ABC1234AB-CD
ABC1234AB-CDE
ABC1234ABC
I need to be able to convert them to their URL equivalent on the manufacturer's website, which are (In the same order):
abc1234_ab
abc1234_ab_cd
abc1234_ab_cde
abc1234_abc
I am looking for a Regex to use with preg_replace that would do the trick.
Thanks in advance!
$output = strtolower(preg_replace('~\d\K(?=[A-Z])|-~', '_', $input));
\K removes that is matched on the left from the match result, so , the digit before the letter is not a part of the match and will not be replaced.
(?=...) is a lookahead assertion that checks if a letter if following, it isn't a part of the match result too and will not be replaced too.
I'm a noob in regular expressions but I`ll give it a shot.
Input: /([A-Z]+)\d+([A-Z]+)\-([A-Z]+)/
A-Z matches uppercase alpha characters
\d matches numbers
"+" is used to repeat
And in the replacement callback use strtolower on the matches and join them how you want :P

PHP RegEx get first letter after set of characters

I have some text with heading string and set of letters.
I need to get first one-digit number after set of string characters.
Example text:
ABC105001
ABC205001
ABC305001
ABCD105001
ABCD205001
ABCD305001
My RegEx:
^(\D*)(\d{1})(?=\d*$)
Link: http://www.regexr.com/390gv
As you cans see, RegEx works ok, but it captures first groups in results also. I need to get only this integer and when I try to put ?= in first group like this: ^(?=\D*)(\d{1})(?=\d*$) , Regex doesn't work.
Any ideas?
Thanks in advance.
(?=..) is a lookahead that means followed by and checks the string on the right of the current position.
(?<=...) is a lookbehind that means preceded by and checks the string on the left of the current position.
What is interesting with these two features, is the fact that contents matched inside them are not parts of the whole match result. The only problem is that a lookbehind can't match variable length content.
A way to avoid the problem is to use the \K feature that remove all on the left from match result:
^[A-Z]+\K\d(?=\d*$)
You're trying to use a positive lookahead when really you want to use non-capturing groups.
The one match you want will work with this regex:
^(?:\D*\d{1})(\d*)$
The (?: string will start a non-capturing group. This will not come back in matches.
So, if you used preg_match(';^(?:\D*\d{1})(\d*)$;', $string, $matches) to find your match, $matches[1] would be the string for which you're looking. (This is because $matches[0] will always be the full match from preg_match.)
try:
^(?:\D*)(\d{1})(?=\d*$) // (?: is the beginning of a no capture group

How to find this regex using PHP

find all the <1></1> and <2></2> and <3></3>... in a string.
<(\d+)></\1>
should work. This ensures that the regex won't match <1></4> for example.
\1 is here a backreference which must match exactly the same string as the first capturing group (the (\d+) in the first angle brackets).
One regex to match any of them?
<([1-3])></\1>
Should there code allow for anything to be posted in between the > and the <? Something like this then:
<([1-3])>(.*?)</\1>
<STYLE[^>]*>([\s\S]*?)<\/STYLE[^>]*>
Just replace STYLE with your tag like 1, 2 whatever.
You can use the following regex: <[0-9]></[0-9]>
EDIT: To avoid that the search matches <2></3> too, you can use a sub-expression and a backreference to instantiate it: <([0-9])></\1>

Categories