PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Thu Sep 21, 2017 4:13 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Regex help please
PostPosted: Sun Sep 21, 2014 3:18 am 
Offline
Forum Newbie

Joined: Sun Sep 21, 2014 3:12 am
Posts: 2
Hi.
Given the string: X1aaX1bbX1ccY
I need to pull out anything between the last X1 and Y. Anything, of any length, is allowed in the aa, bb, or cc positions. I believe it will require a look-ahead or a look-behind, but I can't solve it. I have been pondering for hours. sigh.
e.g.
X1aaX1bbX1ccY should return "cc"
X1aaX1bbX1ccabcY should return "ccabc"
X1aaX1bbX1cc1Y should return "cc1"
etc...
Thanks in advance.


Top
 Profile  
 
 Post subject: Re: Regex help please
PostPosted: Sun Sep 21, 2014 4:32 am 
Offline
Forum Regular
User avatar

Joined: Tue Sep 28, 2010 11:41 am
Posts: 984
Location: Columbus, Ohio
This worked for me:

Syntax: [ Download ] [ Hide ]
^.*X1(.*)Y$


Results from your 3 examples
Syntax: [ Download ] [ Hide ]
                            Start  Length
Match 1:  X1aaX1bbX1ccY        0      13
 Group 1:  cc                 10       2
Match 2:  X1aaX1bbX1ccabcY    15      16
 Group 1:  ccabc              25       5
Match 3:  X1aaX1bbX1cc1Y      33      14
 Group 1:  cc1                43       3


Top
 Profile  
 
 Post subject: Re: Regex help please
PostPosted: Sun Sep 21, 2014 12:58 pm 
Offline
Forum Newbie

Joined: Sun Sep 21, 2014 3:12 am
Posts: 2
Thank you, thank you TwinDev! I'm going to study that and try to understand it. Not sure how it works...? Are the ^ and the $ necessary?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 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