LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-13-2021, 02:45 AM   #1
xg3571
LQ Newbie
 
Registered: Dec 2021
Posts: 12

Rep: Reputation: Disabled
{n,m}? in regex doesn't work as my idea


I coded like:
Code:
namePat = re.compile(r'(Go){1,3}?man')
mo = namePat.search('my name is GoGoGoman')
print(mo.group())
result is like:
GoGoGoman

what I expected:
Goman
 
Old 12-13-2021, 03:15 AM   #2
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,686

Rep: Reputation: Disabled
Matching always starts from the left. GoGoGoman is the first character sequence that can be matched by the pattern.

Last edited by shruggy; 12-13-2021 at 03:16 AM.
 
1 members found this post helpful.
Old 12-13-2021, 03:19 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,157

Rep: Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125
I don't know the python regex engine, but in general if regex doesn't generate what you expect (hope for), PEBKAC applies.
 
1 members found this post helpful.
Old 12-13-2021, 04:04 AM   #4
xg3571
LQ Newbie
 
Registered: Dec 2021
Posts: 12

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by shruggy View Post
Matching always starts from the left. GoGoGoman is the first character sequence that can be matched by the pattern.
But (Go){1,3}? results Go
(Go){1,3}?man results GoGoGoman

I' not still understood
 
Old 12-13-2021, 04:15 AM   #5
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,686

Rep: Reputation: Disabled
As I said, matching always starts on the left side of the string.

When you apply the pattern (Go){1,3}?, it matches the first Go in the string, succeeds, and doesn't look any further.

When you apply the pattern (Go){1,3}?man , the regex engine first tries it on the Go, doesn't succeed as that Go is not followed by man, then tries it on the GoGo, doesn't succeed again, finally, it succeeds at GoGoGoman and stops there.

According to https://www.regular-expressions.info/refadv.html, the standard Python RE engine doesn't support atomic groups, so you're out of luck here. Perl, Ruby, PCRE support them, so you can e.g.
Code:
echo 'GoGoGoman'|grep -Po '(?>(Go){1,3}?)man'
Or you can try https://pypi.org/project/regex which supports atomic grouping.

That said, I don't see how it would be different from matching just Goman.

Last edited by shruggy; 12-13-2021 at 10:37 AM.
 
2 members found this post helpful.
Old 12-13-2021, 04:41 AM   #6
xg3571
LQ Newbie
 
Registered: Dec 2021
Posts: 12

Original Poster
Rep: Reputation: Disabled
Thumbs up

Quote:
Originally Posted by shruggy View Post
As I said, matching always starts on the left side of the string.

When you apply the pattern (Go){1,3}?, it matches the first Go in the string, succeeds, and doesn't look any further.

When you apply the pattern (Go){1,3}?man , the regex engine first tries it on the Go, doesn't succeed as that Go is not followed by man, then tries it on the GoGo, doesn't succeed again, finally, it succeeds at GoGoGoman and stops there.

https://www.regular-expressions.info/refadv.html says me that the standard Python RE engine doesn't support atomic groups, so you're out of luck here. Perl, Ruby, PCRE support them, so you can e.g.
Code:
echo 'GoGoGoman'|grep -Po '(?>(Go){1,3}?)man'
Or you can try https://pypi.org/project/regex which supports atomic grouping.
Thanks for you answer
 
  


Reply

Tags
python, regex



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Confusing issue with Perl regEx - Regex check seems to require variable being set EnderX Programming 1 09-07-2013 04:36 AM
[SOLVED] differences between shell regex and php regex and perl regex and javascript and mysql golden_boy615 Linux - General 2 04-19-2011 01:10 AM
Perl to find regex and print following 5 lines after regex casperdaghost Linux - Newbie 3 08-29-2010 08:08 PM
regex with sed to process file, need help on regex dwynter Linux - Newbie 5 08-31-2007 05:10 AM
Need a regex, I suck at regex's d3funct Programming 4 02-25-2002 08:28 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration