I am trying to search string pattern using regular expression
But since I am very new to regular expression I would like to ask some advice on search pattern
So, I would like to find a line which has ‘value’ as below
value can be both numbers and alphabets
Name (value)
I tried to use pattern
re.search(r"Name \([a-zA-Z0-9]\)", line)
But it doesn’t seem to find as I expected.
How should I write search pattern?
Thank you!
Your expression is fine but your character group lacks a quantifier:
The
+means “at least one of those”. If you don’t put a quantifier it means only one of the mentioned characters may occur. Possible quantifiers are:+for one or more,?for at most one and*for any number of those{x,y}works as well, meaning at leastx, but at mostycharactersAnd the quantifier always refers to what you place right before it, which might be a single character or a character group enclosed in
[].Edit: As mentioned in the comments by root you might want to look at
\w, which is a short notation for “all word characters”. It includes all letters and digits, as well as the underscore and maybe special characters if they are specially defined by the current locale.