PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Fri Oct 19, 2018 5:16 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Tue Aug 07, 2012 7:52 am 
Offline
Forum Newbie

Joined: Tue Aug 07, 2012 7:36 am
Posts: 2
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


Top
 Profile  
 
PostPosted: Tue Aug 07, 2012 4:07 pm 
Offline
Forum Commoner
User avatar

Joined: Thu Dec 15, 2011 2:40 pm
Posts: 85
Location: Nelson, NZ
Hi Michael,

To get started, use something like
Syntax: [ Download ] [ Hide ]
$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
Syntax: [ Download ] [ Hide ]
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:
Syntax: [ Download ] [ Hide ]
$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


Top
 Profile  
 
PostPosted: Tue Aug 07, 2012 8:05 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
Another approach is with preg_split:
Syntax: [ Download ] [ Hide ]
$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.
Syntax: [ Download ] [ Hide ]
array_filter(preg_split(...))


Top
 Profile  
 
PostPosted: Tue Aug 07, 2012 8:51 pm 
Offline
Forum Commoner
User avatar

Joined: Thu Dec 15, 2011 2:40 pm
Posts: 85
Location: Nelson, NZ
Nice idea! :)


Top
 Profile  
 
PostPosted: Wed Aug 08, 2012 1:53 am 
Offline
Forum Newbie

Joined: Tue Aug 07, 2012 7:36 am
Posts: 2
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group