0

if i set the variable a to

a = "6253625879615786"

if i use the regex statement

print(re.findall(r'^[456][0-9]{15}[^,_]',a))

I do not get any output but if i use the regex statement with the * at the end to 0 or 1 times

print(re.findall(r'^[456][0-9]{15}[^,_]*',a))         

Why is this?

Aaron B
  • 49
  • 4
  • 1
    Your input string is 16 char long. Pattern 1 needs 17 chars to match, while the second one needs 16. `*` means *0 or more occurrences*. Study [quantifiers](https://www.regular-expressions.info/repeat.html). – Wiktor Stribiżew May 04 '18 at 23:04
  • `*` means "any" number of times, including 0. – calico_ May 04 '18 at 23:05
  • but without the * in pattern one, how does it require 17 chars if I am trying to just omit those the ^, and_ from happening and not actually occur in the string – Aaron B May 04 '18 at 23:12
  • the first pattern says it ends with a character that is not , or _ that's a lot of possibilities – Kevin May 04 '18 at 23:22
  • Oh,so with the *, if i have a comma or underscore anywhere in the a variable, it will not print but without the *, regex interprets [^,_] as in the last part of the string should not contain the comma or underscore – Aaron B May 04 '18 at 23:24
  • so could i have just used a negative look back like (?!,|_) instead – Aaron B May 04 '18 at 23:33

0 Answers0