0

In Python 2.7, when i try re.findall("abc.","abcd abc"), I receive the expected answer "abcd" only

if I use re.findall("abc.asterisk*","abcd abc") I receive "abcd" "abc" as expected but when I use re.findall('abc.+',"abcd abc") I receive "abcd" "abc" again instead of just "abcd"

Why?

  • 1
    Visit an [explainer site](https://regex101.com). This is really fundamental to how a regular expression works. `+` means "one or more" while `*` means "zero or more". – tadman Sep 18 '17 at 17:21
  • It's not very clear what you're asking but `.*` will match between 0 and unlimited of any character (except new line) and `.+` will match between ` and unlimited of the same character set. – ctwheels Sep 18 '17 at 17:22
  • You don't receive "abcd" and "abc" but the unique whole string "abcd abc". – Casimir et Hippolyte Sep 18 '17 at 17:23
  • @tadman that's is correct, it's not the same as "exactly one match", you're right – Damián Rafael Lattenero Sep 18 '17 at 17:27

1 Answers1

0

As the comments says, * is for 0 or more matches, + is for 1 or more matches.

If you want exactly 1 match, you shuld do directly like:

abc.
Damián Rafael Lattenero
  • 14,625
  • 3
  • 30
  • 62