By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,882 Members | 2,453 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,882 IT Pros & Developers. It's quick & easy.

Why does re.sub('.*?', '-', 'abc') return '-a-b-c-' instead of '-------'?

P: 1
This is the results from python2.7.

>>> re.sub('.*?', '-', 'abc')
The results I thought should be as follows.

>>> re.sub('.*?', '-', 'abc')
But it's not. Why?
Aug 1 '18 #1
Share this Question
Share on Google+
1 Reply

Expert Mod 5K+
P: 5,397
Reference: Regular expression operations

Breaking down the expression
. - dot ignore newline match all other characters
* - match zero or more repetitions of the proceeding RE
such that "ab*" will match "ab" "abb" "abbbb" etc...
? - match zero or 1 repetitions of the proceeding RE
such that "ab?" it will match "a" or "ab"

So we now have the construct *?
Adding the ? after the * says only match the first instance.
That is to say * by itself will match as many as possible - it's greedy.
*? is not greedy and matches as few as possible.
SO for string="abc"; rex=".*?"; will match only the "a"

Change the expression to - with global and multi set true:
this should render to ' - - - '

I know that this will render "---" also with global and multi set true:

You need a good primer on Regular Expressions

>I've found this one:
It appears to cover the basics and a bit of extra too.
Aug 6 '18 #2

Post your reply

Sign in to post your reply or Sign up for a free account.