0

I'm using regex to find a car plate inside of a given input from a user,

numberPlate = input("Enter the Number plate of the car, eg LV04 HNX, HR06PRK")
numberPlateRegEx = re.compile(r'\w\w\d\d( )?\w\w\w') # Creates a regular expression object, can be used to scan strings for anything matching the given pattern, \w is a letter, \d is (strictly, a number or letter), ( )? means there can be an optional space
numberPlateFound = re.findall(numberPlateRegEx, numberPlate)

When I enter an input including a pattern including a car number plate then numberPlateFound is a list with a single space inside of it:

And when I enter an input not including a car number plate inside:

This works if i want to just find if something was found, but what If i want to have the found pattern returned? Would I use a different method?

EDIT: My question is different from this suggested question as in my example it returns not an empty string, but a string with a space character inside, and I dont know why, I want to know why

  • Possible duplicate of [Python re.findall behaves weird](https://stackoverflow.com/questions/31915018/python-re-findall-behaves-weird) – Sebastian Proske Feb 13 '18 at 18:51
  • Concerning the edit: It's still the same issue as shown in the dupe - your pattern contains capturing groups, so findall returns only the captured content. Solution is also the same - use non-capturing groups. – Sebastian Proske Feb 13 '18 at 18:58
  • Change the expression to `r'\w\w\d\d(?: )?\w\w\w'`, `r'\w\w\d\d ?\w\w\w'`, or `r'\w\w\d\d\s?\w\w\w'` – Patrick Haugh Feb 13 '18 at 18:59
  • Thanks to both of you, I think i need to look into regex more haha, i understand :) –  Feb 13 '18 at 19:01

1 Answers1

1

A few things:

  • No need to put space in its own group to make it optional.
  • Without \b as my pattern below shows, you may match more than you're trying to.
    • \b is a word boundary. It matches anywhere between a word character and non-word character or line start/end: (^\w|\w$|\W\w|\w\W) without consuming characters (zero-width assertion)
  • Using quantifiers as my pattern below shows improves performance

See code in use here

import re

r = re.compile(r"\b\w{2}\d{2} ?\w{3}\b")
s = "Enter the Number plate of the car, eg LV04 HNX, HR06PRK"
print(r.findall(s))
ctwheels
  • 21,901
  • 9
  • 42
  • 77