473,836 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to write Regular Expression for recursive matching?

Hi All,

I have problem to split a string like this:

'abc.defg.hij.k lmnop'

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,

Nov 26 '07 #1
6 3845
On Nov 26, 10:40 am, lisong <lisong.1...@gm ail.comwrote:
Hi All,

I have problem to split a string like this:

'abc.defg.hij.k lmnop'

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'.split( '.')
['abc', 'defg', 'hij', 'klmnop']

-- Paul
Nov 26 '07 #2
lisong wrote:
Hi All,

I have problem to split a string like this:

'abc.defg.hij.k lmnop'

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.k lmnop'

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

Diez
Nov 26 '07 #3
On Nov 26, 10:51 am, Paul McGuire <pt...@austin.r r.comwrote:
On Nov 26, 10:40 am, lisong <lisong.1...@gm ail.comwrote:


Hi All,
I have problem to split a string like this:
'abc.defg.hij.k lmnop'
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'.split(' .')

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

-- Paul- Hide quoted text -

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

-- Paul
Nov 26 '07 #4
lisong wrote:
Hi All,

I have problem to split a string like this:

'abc.defg.hij.k lmnop'

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 ?
>>def f(s) :
ws = s.split('.')
return map('.'.join,zi p(ws,ws[1:]))
>>f('abc.defg.h ij.klmnop')
['abc.defg', 'defg.hij', 'hij.klmnop']

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

I have problem to split a string like this:

'abc.defg.hij.k lmnop'

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.k lmnop'

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
Nov 26 '07 #6
On Nov 26, 12:34 pm, "J. Clifford Dyer" <j...@sdf.lones tar.orgwrote:
On Mon, Nov 26, 2007 at 06:04:54PM +0100, Diez B. Roggisch wrote regarding Re: How to write Regular Expression for recursive matching?:


lisong wrote:
Hi All,
I have problem to split a string like this:
'abc.defg.hij.k lmnop'
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.k lmnop'
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
Nov 26 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
7288
by: Erik Lechak | last post by:
Hello all, I wrote the code below. It is simply a dictionary that uses regular expressions to match keys. A quick look at _test() will give you an example. Is there a module that already does this? Is there a way and would it be better to use list comprehension? (using python 2.3) Just looking for a better or more pythonic way to do it.
3
2033
by: Tom | last post by:
I have struggled with the issue of whether or not to use Regular Expressions for a long time now, and after implementing many text manipulating solutions both ways, I've found that writing specialized code instead of an RE is almost always the better solution. Here is why.... RE's are complex. Sure it is one line of code, but it is on hell of a line. Some of my RE remind me of the obfuscated code contest winners, where one line of...
7
3836
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I want to avoid that. My question here is if there is a way to pass either a memory stream or array of "find", "replace" expressions or any other way to avoid multiple copies of a string. Any help will be highly appreciated
25
5184
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART (CONDUCTION DEFECT) 37.33/2 HEART (CONDUCTION DEFECT) WITH CATHETER 37.34/2 " the expression is "HEART (CONDUCTION DEFECT)". How do I gain access to the expression (not the matches) at runtime? Thanks, Mike
5
2325
by: Avi Kak | last post by:
Folks, Does regular expression processing in Python allow for executable code to be embedded inside a regular expression? For example, in Perl the following two statements $regex = qr/hello(?{print "saw hello\n"})mello(?{print "saw mello\n"})/; "jellohellomello" =~ /$regex/;
14
2285
by: Chris | last post by:
I need a pattern that matches a string that has the same number of '(' as ')': findall( compile('...'), '42^((2x+2)sin(x)) + (log(2)/log(5))' ) = Can anybody help me out? Thanks for any help!
3
2763
by: Zeba | last post by:
Hi guys, I need some help regarding regular expressions. Consider the following statement : System.Text.RegularExpressions.Match match = System.Text.RegularExpressions.Regex.Match(requestPath, "(*?\ \.ashx)"); (where requestPath is a string)
7
4408
by: blaine | last post by:
Hey everyone, For the regular expression gurus... I'm trying to write a string matching algorithm for genomic sequences. I'm pulling out Genes from a large genomic pattern, with certain start and stop codons on either side. This is simple enough... for example: start = AUG stop=AGG BBBBBBAUGWWWWWWAGGBBBBBB
9
2797
by: netimen | last post by:
I have a text containing brackets (or what is the correct term for '>'?). I'd like to match text in the uppermost level of brackets. So, I have sth like: 'aaaa 123 < 1 aaa < t bbb < a <tt ff 2 > bbbbb'. How to match text between the uppermost brackets ( 1 aaa < t bbb < a <tt ff 2 )? P.S. sorry for my english.
0
9810
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10819
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10526
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10567
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5641
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5811
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4437
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4000
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3100
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.