How to write Regular Expression for recursive matching?

L

lisong

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,
 
P

Paul McGuire

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:
['abc', 'defg', 'hij', 'klmnop']

-- Paul
 
B

Boris Borcic

lisong said:
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 ?
ws = s.split('.')
return map('.'.join,zip(ws,ws[1:]))
['abc.defg', 'defg.hij', 'hij.klmnop']
 
D

Diez B. Roggisch

lisong said:
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
 
P

Paul McGuire

I have problem to split a string like this:

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?

Why are you using regular expressions? Use the split method defined
for strings:

['abc', 'defg', 'hij', 'klmnop']

-- Paul- Hide quoted text -

- Show quoted text -

Sorry, misread your post - Diez Roggisch has the right answer.

-- Paul
 
J

J. Clifford Dyer

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
 
L

lisong

lisong wrote:
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

Thank u all for your kindly reply, I agree, RE is not necessary here.

Song
 

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,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top