Regex (matching all, except the given String)

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
mikelll
Forum Newbie
Posts: 2
Joined: Tue Aug 07, 2012 7:36 am

Regex (matching all, except the given String)

Post by mikelll »

Hi guys,

i am trying to get this regex running for several hours, but it wont work :(.
I have to match all Strings except the ones with AB_*

Source Text: one, two, three, AB_user, four, five, six, AB_admin, seven, eight, nine, AB_michael, ten, AB_Stephanie, eleven, twelve, test
Match Pattern: .*?(?=AB_.*?,)

Result:
matches Array:(
[0] => Array
(
[0] => one, two, three,
[1] =>
[2] => AB_user, four, five, six,
[3] =>
[4] => AB_admin, seven, eight, nine,
[5] =>
[6] => AB_michael, ten,
[7] =>
)

but thats wrong!

The correct result is:

[0] => one, two, three,
[1] => four, five, six,
[2] => seven, eight, nine
[3] => ten
[4] => eleven, twelve, test

Please help!

greetz, mike
User avatar
ragax
Forum Commoner
Posts: 85
Joined: Thu Dec 15, 2011 1:40 pm
Location: Nelson, NZ

Re: Regex (matching all, except the given String)

Post by ragax »

Hi Michael,

To get started, use something like

Code: Select all

$regex='~(.*?)AB_\w+\s*,\s*~';
The parentheses capture the text you want in Group 1.
Now use preg_match_all on the target string, e.g. preg_match_all($regex,$target,$matches)

$matches[1] will contain all the group 1 captures.
To check that run something like

Code: Select all

if(preg_match_all($regex,$string,$m)) var_dump( $m[1]);
This will return all the groups except for the last one (eleven, twelve...), but it's a good starting point to see how it works.

If you want all the groups in a single regex, you can use two capture groups: one for the first array element, the other for the others:

Code: Select all

$regex = $regex='~(.*?)?AB_\w+\s*,\s*(.*?)(?=AB|$)~';
The first string will be the first and only match of group 1: $m[1][0].
The others will be in group 2: the $m[2] array.

This uses a lookahead, you may want to have a look at the lookahead page on my tut.

All the best
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Regex (matching all, except the given String)

Post by requinix »

Another approach is with preg_split:

Code: Select all

$groups = preg_split('/(AB_\S+(,\s*|$))+/', $string);
Should throw in an array_filter() for good measure though - eliminates empty groups if the string starts or ends with that AB_* thing.

Code: Select all

array_filter(preg_split(...))
User avatar
ragax
Forum Commoner
Posts: 85
Joined: Thu Dec 15, 2011 1:40 pm
Location: Nelson, NZ

Re: Regex (matching all, except the given String)

Post by ragax »

Nice idea! :)
mikelll
Forum Newbie
Posts: 2
Joined: Tue Aug 07, 2012 7:36 am

Re: Regex (matching all, except the given String)

Post by mikelll »

Thx for that!!!

But i have the problem, that i can only use the .ReplaceAllMatches(String source, Regex regex, String replaceString) from the API, because thats the only one, who is implemented in the Software. Is it possible to solve that with that function?

The Goal is to get the ResultString

AB_user, AB_admin, AB_michael, AB_Stephanie,

so i have to replace the one, two, three, four, five, six...etc. with blank or whitespace characters.

I think its difficult to solve that with one function
Post Reply