473,396 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Non greedy regex

Can someone please explain why these expressions both produce the same
result? Surely this means that non-greedy regex does not work?

print re.sub( 'a.*b', '', 'ababc' )

gives: 'c'

Understandable. But

print re.sub( 'a.*?b', '', 'ababc' )

gives: 'c'

NOT, understandable. Surely the answer should be: 'abc'

Dec 14 '06 #1
3 1956
On Thu, 2006-12-14 at 06:45 -0800, ti*************@gmail.com wrote:
Can someone please explain why these expressions both produce the same
result? Surely this means that non-greedy regex does not work?

print re.sub( 'a.*b', '', 'ababc' )

gives: 'c'

Understandable. But

print re.sub( 'a.*?b', '', 'ababc' )

gives: 'c'

NOT, understandable. Surely the answer should be: 'abc'
You didn't tell re.sub to only substitute the first occurrence of the
pattern. It non-greedily matches two occurrences of 'ab' and replaces
each of them with ''. Try replacing with some non-empty string instead
to observe the difference between the two behaviors.

-Carsten
Dec 14 '06 #2
There's an optional count argument that will give what you want. Try
re.sub('a.*b','','ababc',count=1)
Carsten Haese wrote:
On Thu, 2006-12-14 at 06:45 -0800, ti*************@gmail.com wrote:
Can someone please explain why these expressions both produce the same
result? Surely this means that non-greedy regex does not work?

print re.sub( 'a.*b', '', 'ababc' )

gives: 'c'

Understandable. But

print re.sub( 'a.*?b', '', 'ababc' )

gives: 'c'

NOT, understandable. Surely the answer should be: 'abc'

You didn't tell re.sub to only substitute the first occurrence of the
pattern. It non-greedily matches two occurrences of 'ab' and replaces
each of them with ''. Try replacing with some non-empty string instead
to observe the difference between the two behaviors.

-Carsten
Dec 14 '06 #3
print re.sub( 'a.*?b', '', 'ababc' )

gives: 'c'

you've forgot "^" at the begging of the pattern (there is another 'ab' before 'c')
print re.sub( '^a.*?b', '', 'ababc' )
ti*************@gmail.com wrote:
Can someone please explain why these expressions both produce the same
result? Surely this means that non-greedy regex does not work?

print re.sub( 'a.*b', '', 'ababc' )

gives: 'c'

Understandable. But

print re.sub( 'a.*?b', '', 'ababc' )

gives: 'c'

NOT, understandable. Surely the answer should be: 'abc'

--
Maksim Kasimov
Dec 14 '06 #4

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

Similar topics

6
by: Jack Smith | last post by:
Hello, any help appreciated with following problem. I figured out the algorithm (I think), just having trouble proving it is optimal. Suppose we are given n tasks each of which takes 1 unit...
3
by: alexk | last post by:
Hi, I would like to request your help. My problem is as follows. I want to match urls, and therefore I have a group of long valid domain names in my regex: ........
12
by: lothar | last post by:
re: 4.2.1 Regular Expression Syntax http://docs.python.org/lib/re-syntax.html *?, +?, ?? Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few...
2
by: Wole | last post by:
Hi. I really need help with a regular expression in ASP. I need a regex pattern that will grab a select or textarea tag in an HTML file. If I run the pattern I should get <select...
3
by: Jane Doe | last post by:
Hello, I need to browse a list of hyperlinks, each followed by an author, and remove the links only for certain authors. 1. I searched the archives on Google, but didn't find how to tell the...
14
by: santosh | last post by:
Hello all I want to know whether there is any greedy approach for job sequencing with variable job completion times.. if there is no greedy approach how to prove it...
8
by: John Hazen | last post by:
I want to match one or two instances of a pattern in a string. According to the docs for the 're' module ( http://python.org/doc/current/lib/re-syntax.html ) the '?' qualifier is greedy by...
6
by: EXI-Andrews, Jack | last post by:
the '*' and '+' don't seem to be greedy.. they will consume less in order to match a string: ('aa', 'ab') this is the sort of behaviour i'd expect from '(a+?)(ab)' a+ should greedily...
3
by: =?Utf-8?B?c2JwYXJzb25z?= | last post by:
I have a scenario where a string is sent in chunks to my app. I need to be able to identify certain tags in this partial string as it arrives. eg <DALFile>xxxxxxxxx</DALFile> I need to be able...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.