Connecting Tech Pros Worldwide Forums | Help | Site Map

How to write Regular Expression for recursive matching?

lisong
Guest
 
Posts: n/a
#1: Nov 26 '07
Hi All,

I have problem to split a string like this:

'abc.defg.hij.klmnop'

and I want to get all substrings with only one '.' in mid. so the
output I expect is :

'abc.defg', 'defg.hij', 'hij.klmnop'

a simple regular expression '\w+.\w' will only return:
'abc.defg', 'hij.klmnop'

is there a way to get 'defg.hij' using regular expression?

Thanks,


Paul McGuire
Guest
 
Posts: n/a
#2: Nov 26 '07

re: How to write Regular Expression for recursive matching?


On Nov 26, 10:40 am, lisong <lisong.1...@gmail.comwrote:
Quote:
Hi All,
>
I have problem to split a string like this:
>
'abc.defg.hij.klmnop'
>
and I want to get all substrings with only one '.' in mid. so the
output I expect is :
>
'abc.defg', 'defg.hij', 'hij.klmnop'
>
a simple regular expression '\w+.\w' will only return:
'abc.defg', 'hij.klmnop'
>
is there a way to get 'defg.hij' using regular expression?
>
Thanks,
Why are you using regular expressions? Use the split method defined
for strings:
Quote:
Quote:
Quote:
>>'abc.defg.hij.klmnop'.split('.')
['abc', 'defg', 'hij', 'klmnop']

-- Paul
Diez B. Roggisch
Guest
 
Posts: n/a
#3: Nov 26 '07

re: How to write Regular Expression for recursive matching?


lisong wrote:
Quote:
Hi All,
>
I have problem to split a string like this:
>
'abc.defg.hij.klmnop'
>
and I want to get all substrings with only one '.' in mid. so the
output I expect is :
>
'abc.defg', 'defg.hij', 'hij.klmnop'
>
a simple regular expression '\w+.\w' will only return:
'abc.defg', 'hij.klmnop'
>
is there a way to get 'defg.hij' using regular expression?
Nope. Regular expressions can't get back in their input-stream, at least not
for such stuff.

The problem at hand is easily solved using

s = 'abc.defg.hij.klmnop'

pairs = [".".join(v) for v in zip(s.split(".")[:-1], s.split(".")[1:])]

Diez
Paul McGuire
Guest
 
Posts: n/a
#4: Nov 26 '07

re: How to write Regular Expression for recursive matching?


On Nov 26, 10:51 am, Paul McGuire <pt...@austin.rr.comwrote:
Quote:
On Nov 26, 10:40 am, lisong <lisong.1...@gmail.comwrote:
>
>
>
>
>
Quote:
Hi All,
>
Quote:
I have problem to split a string like this:
>
Quote:
'abc.defg.hij.klmnop'
>
Quote:
and I want to get all substrings with only one '.' in mid. so the
output I expect is :
>
Quote:
'abc.defg', 'defg.hij', 'hij.klmnop'
>
Quote:
a simple regular expression '\w+.\w' will only return:
'abc.defg', 'hij.klmnop'
>
Quote:
is there a way to get 'defg.hij' using regular expression?
>
Quote:
Thanks,
>
Why are you using regular expressions? Use the split method defined
for strings:
>
Quote:
Quote:
>'abc.defg.hij.klmnop'.split('.')
>
['abc', 'defg', 'hij', 'klmnop']
>
-- Paul- Hide quoted text -
>
- Show quoted text -
Sorry, misread your post - Diez Roggisch has the right answer.

-- Paul
Boris Borcic
Guest
 
Posts: n/a
#5: Nov 26 '07

re: How to write Regular Expression for recursive matching?


lisong wrote:
Quote:
Hi All,
>
I have problem to split a string like this:
>
'abc.defg.hij.klmnop'
>
and I want to get all substrings with only one '.' in mid. so the
output I expect is :
>
'abc.defg', 'defg.hij', 'hij.klmnop'
>
a simple regular expression '\w+.\w' will only return:
'abc.defg', 'hij.klmnop'
>
is there a way to get 'defg.hij' using regular expression?
>
Thanks,
>
Do you need it to be a regular expression ?
Quote:
Quote:
Quote:
>>def f(s) :
ws = s.split('.')
return map('.'.join,zip(ws,ws[1:]))
Quote:
Quote:
Quote:
>>f('abc.defg.hij.klmnop')
['abc.defg', 'defg.hij', 'hij.klmnop']

J. Clifford Dyer
Guest
 
Posts: n/a
#6: Nov 26 '07

re: How to write Regular Expression for recursive matching?


On Mon, Nov 26, 2007 at 06:04:54PM +0100, Diez B. Roggisch wrote regarding Re: How to write Regular Expression for recursive matching?:
Quote:
>
lisong wrote:
>
Quote:
Hi All,

I have problem to split a string like this:

'abc.defg.hij.klmnop'

and I want to get all substrings with only one '.' in mid. so the
output I expect is :

'abc.defg', 'defg.hij', 'hij.klmnop'

a simple regular expression '\w+.\w' will only return:
'abc.defg', 'hij.klmnop'

is there a way to get 'defg.hij' using regular expression?
>
Nope. Regular expressions can't get back in their input-stream, at least not
for such stuff.
>
The problem at hand is easily solved using
>
s = 'abc.defg.hij.klmnop'
>
pairs = [".".join(v) for v in zip(s.split(".")[:-1], s.split(".")[1:])]
>
which is veritably perlesque in its elegance and simplicity!

A slightly more verbose version.

l = s.split('.')
pairs = []
for x in xrange(len(l)-1):
pairs.append('.'.join(l[x:x+2]))

Cheers,
Cliff
lisong
Guest
 
Posts: n/a
#7: Nov 26 '07

re: How to write Regular Expression for recursive matching?


On Nov 26, 12:34 pm, "J. Clifford Dyer" <j...@sdf.lonestar.orgwrote:
Quote:
On Mon, Nov 26, 2007 at 06:04:54PM +0100, Diez B. Roggisch wrote regarding Re: How to write Regular Expression for recursive matching?:
>
>
>
>
>
Quote:
lisong wrote:
>
Quote:
Quote:
Hi All,
>
Quote:
Quote:
I have problem to split a string like this:
>
Quote:
Quote:
'abc.defg.hij.klmnop'
>
Quote:
Quote:
and I want to get all substrings with only one '.' in mid. so the
output I expect is :
>
Quote:
Quote:
'abc.defg', 'defg.hij', 'hij.klmnop'
>
Quote:
Quote:
a simple regular expression '\w+.\w' will only return:
'abc.defg', 'hij.klmnop'
>
Quote:
Quote:
is there a way to get 'defg.hij' using regular expression?
>
Quote:
Nope. Regular expressions can't get back in their input-stream, at least not
for such stuff.
>
Quote:
The problem at hand is easily solved using
>
Quote:
s = 'abc.defg.hij.klmnop'
>
Quote:
pairs = [".".join(v) for v in zip(s.split(".")[:-1], s.split(".")[1:])]
>
which is veritably perlesque in its elegance and simplicity!
>
A slightly more verbose version.
>
l = s.split('.')
pairs = []
for x in xrange(len(l)-1):
pairs.append('.'.join(l[x:x+2]))
>
Cheers,
Cliff
Thank u all for your kindly reply, I agree, RE is not necessary here.

Song
Closed Thread