Page 1 of 1

regex - text that contains other text

Posted: Tue May 19, 2009 3:40 pm
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

Re: regex - text that contains other text

Posted: Wed May 20, 2009 9:14 am
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
 

Re: regex - text that contains other text

Posted: Wed May 20, 2009 9:23 am
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

Re: regex - text that contains other text

Posted: Thu May 21, 2009 2:19 am
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

Re: regex - text that contains other text

Posted: Thu May 21, 2009 3:24 am
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.

Re: regex - text that contains other text

Posted: Thu May 21, 2009 4:45 am
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.

Re: regex - text that contains other text

Posted: Thu May 21, 2009 4:51 am
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

Re: regex - text that contains other text

Posted: Thu May 21, 2009 5:18 am
by prometheuzz
Try:

Code: Select all

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