regex - text that contains other text

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

Moderator: General Moderators

Post Reply
michalmas
Forum Newbie
Posts: 14
Joined: Wed Apr 29, 2009 9:04 am

regex - text that contains other text

Post by michalmas »

Hello,

On the text i have used reg exp - with it, i can match all struc - endstruc that don't contain elem text. However, i can't transform it to condition that it does contain.

Can you help?

The text is:

Code: Select all

some text is here ble bla blu
now there is some 
elem xsxsx
struc something
now we are inside and next element:
elem value
elem
something els, and again
elem val2
end struc
this is not inside
elem valXXX
struc 
asasas
end struc
The expession is:

Code: Select all

struc((?:(?!elem).)*)end\sstruc
Now i capture

Code: Select all

struc 
asasas
end struc
But i want to capture:

Code: Select all

struc something
now we are inside and next element:
elem value
elem
something els, and again
elem val2
end struc
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Re: regex - text that contains other text

Post by GeertDD »

Code: Select all

 
struc.*?elem.*?end\sstruc
 
Personally, I would probably go for a regex like the one below and then loop over the matches and search for "elem" separately.

Code: Select all

 
struc.*?end\sstruc
 
michalmas
Forum Newbie
Posts: 14
Joined: Wed Apr 29, 2009 9:04 am

Re: regex - text that contains other text

Post by michalmas »

It must be one expression. Unfortunately...

I also tried:

Code: Select all

struc((?:(?[b]=[/b]elem).)*)end\sstruc
but it doesn't work...

Any other solutions?

Best,
Michal
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: regex - text that contains other text

Post by prometheuzz »

michalmas wrote:...

Any other solutions?

Best,
Michal
Geert's first suggestion will work just fine. Note that you will have to enable the DOT-ALL flag:

Code: Select all

'/a-regex/s' // the s-flag is the DOT-ALL flag
michalmas
Forum Newbie
Posts: 14
Joined: Wed Apr 29, 2009 9:04 am

Re: regex - text that contains other text

Post by michalmas »

@ prometheuzz:
I believe GeertDD has edited the post later.

But the result is not really what i want to get. For instance, for the input:

Code: Select all

some text is here ble bla blu
now there is some 
elem xsxsx
struc something
now we are inside and next element:
elem value
something els, and again
elem val2
end struc
this is not inside
elem valXXX
struc 
asasas
end struc
struc 
elem xpo
end struc
sdffdsd
 
i get:

Code: Select all

struc something
now we are inside and next element:
elem value
something els, and again
elem val2
end struc[b]
struc 
asasas
end struc[/b]
struc 
elem xpo
end struc
It was too greedy. The bold part should not be returned.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: regex - text that contains other text

Post by prometheuzz »

michalmas wrote:@ prometheuzz:
I believe GeertDD has edited the post later.

But the result is not really what i want to get. For instance, for the input:

Code: Select all

some text is here ble bla blu
now there is some 
elem xsxsx
struc something
now we are inside and next element:
elem value
something els, and again
elem val2
end struc
this is not inside
elem valXXX
struc 
asasas
end struc
struc 
elem xpo
end struc
sdffdsd
 
i get:

Code: Select all

struc something
now we are inside and next element:
elem value
something els, and again
elem val2
end struc[b]
struc 
asasas
end struc[/b]
struc 
elem xpo
end struc
It was too greedy. The bold part should not be returned.
I don't see any bold part.
michalmas
Forum Newbie
Posts: 14
Joined: Wed Apr 29, 2009 9:04 am

Re: regex - text that contains other text

Post by michalmas »

Probably tag has not worked.

So, from text:

Code: Select all

struc something
now we are inside and next element:
elem value
something els, and again
elem val2
end struc
struc
asasas
end struc
struc
elem xpo
end struc
the wrong part is:

Code: Select all

struc
asasas
end struc
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: regex - text that contains other text

Post by prometheuzz »

Try:

Code: Select all

'/struc(?=(?:(?!end\sstruc).)*elem)(?:(?!struc).)*end\sstruc/s'
Post Reply