How to write Regular Expression for recursive matching? | | |
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, | | | | 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 | | | | 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 | | | | 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:
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?
> >
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 | | | | 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 ? ws = s.split('.')
return map('.'.join,zip(ws,ws[1:])) Quote: Quote: Quote:
>>f('abc.defg.hij.klmnop')
['abc.defg', 'defg.hij', 'hij.klmnop'] | | | | 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 | | | | 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: 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 |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|