I am trying to capture a value out of a string. The string’s format should be
01+XXXX
and I want to capture XXXX using a regular expression. This is what I came up with –
01+\\s*(?<1>[.0-9]*)
But that won’t work. What DOES work is –
01+\\s*(?<1>[+.0-9]*)
The only difference is adding the + into the character class. My main question is – why does the second expression work and the first expression doesn’t? In the first one, I look for 01+ and the rest of it should go to [.0-9]. It seems to me that the second one wants to read + twice – is that not what its doing? I am pretty new to regular expressions so I feel like I might be missing something small.
On this site http://www.codeproject.com/Articles/9099/The-30-Minute-Regex-Tutorial it says that + is used for “Repeat one or more times”. So is it trying to read 01+ more than once?
It’s reading the
1one or more times. That is, the regex01+matches01or011or0111etc.But it doesn’t match the
+. If you want to match a literal+, write01\+or01[+]for the regex.