substring and regular expression

B

borges2003xx

i have read
finding sublist
http://groups.google.it/group/comp....85256/5156ada81fc9358a?hl=it#5156ada81fc9358a


the problem was in a string to find if we have two substring non
overlapping of lenght al least 4

it was done by
r=re.compile(r'(?P<seq>.{4,}).*(?P=seq)')

my knowhow on re is null
there is a way in re for ask if a substring is the reverse of the
otherone? First seq is second seq in reverse order

i appreciate all persons that can help me.
excuse my ignorance
 
K

Kent Johnson

i have read
finding sublist
http://groups.google.it/group/comp....85256/5156ada81fc9358a?hl=it#5156ada81fc9358a


the problem was in a string to find if we have two substring non
overlapping of lenght al least 4

it was done by
r=re.compile(r'(?P<seq>.{4,}).*(?P=seq)')

my knowhow on re is null
there is a way in re for ask if a substring is the reverse of the
otherone? First seq is second seq in reverse order

For a fixed-length substring you can use brute force. Here is a regex that matches a string containing a four-character substring and its reverse:
(?P<s1>.)(?P<s2>.)(?P<s3>.)(?P<s4>.).*(?P=s4)(?P=s3)(?P=s2)(?P=s1)

You might want to look at the Regular Expression HOW-TO document:
http://www.amk.ca/python/howto/regex/

Kent
 
B

borges2003xx

but in general is there a way to include in a re, in this example
something like...matches iff p , and q in which p==q[::-1] ? A way to
putting a small part of code of python in re? Thanx for your many helps
 
D

Diez B.Roggisch

borges2003xx said:
but in general is there a way to include in a re, in this example
something like...matches iff p , and q in which p==q[::-1] ? A way to
putting a small part of code of python in re? Thanx for your many helps

What you are after is a parser - there are plenty available. I prefer spark, but
pyparsing has gained lots of attention lately and is AFAIK the quasi-standard.

The problem you described is a classical instance of a so-called
context-free grammar.
There is absolutely _no_ way to teach regular expressions how to detect words
created based on a grammar of that kind. Really. It won't work.

So - either use a parser, or write a simple one yourself - the code above
qualifies as an attempt to do so.

Regards,

Diez
 
B

borges2003xx

can you be so kind to show me a small example in spark if u like for
remove all string with a substring of length 4 and the reverse of it?
i thank you a lot
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top