I have some strings that begin with ABC123
(simplified example for demonstration – actual strings may not be numbers or letters). From each string, I want to capture the character after ABC
(1
) and the character 2 digits after that (3
), without capturing 2
. I’ve managed to do the first part using a lookbehind, but haven’t been able to figure out the rest.
Regex101 here with what I’ve got so far.
3
Answers
For completeness here's the version of hanlog's answer I ended up using:
I removed the digit requirement for the non-capturing group and the names for the captured groups to help improve performance and/or memory, but other than that it's basically the same solution.
I created an array of two elements inside your function. The elements are initialized with
false
, which means that the character was not found.Then I compute the index of "ABC" to make sure it’s inside your array. It could and should be a parameter, I hard-coded it in this case for the sake of simplicity. Then I check for each character whether its index is inside the string and if so, then fill the output appropriately.
Finally I return the output.
I would consider using two named capture groups with a non-capturing group in between. Here is an example:
(?<=ABC), is a positive lookbehind that will match anything after the pattern ABC.
(?<match1>.), is a named capturing group that will catch any single character using the "." pattern.
(?>d{1}), is a non-capturing group that will match, but not catch, its pattern. The pattern is a single digit, "d{1}", but here you could add something else that will fit your needs.
(?<match2>.), is the second named capturing group, which will match any single character.
To use this is PHP you could write like this: