This is more a question of understanding than an actual problem. The situation explains as follows. I got some float numbers (e.g. an amount of money) between two quotation marks "".
Examples:
- "1,23"
- "12,23"
- "123,23"
Now I wanted to match the comma in those expressions. I built the following regex which works for me:
(?<=\"[0-9]|[0-9]{2})(,)(?=[0-9]{2}\")
The part which I don't completly understand is the lookbehind in combination with the or "|". But let's break it up:
(
?<= //Start of the lookbehind
\" //Starting with an escaped quotation mark "
[0-9] //Followed by a digit between 0 and 9
Now I had the problem, that after the quotation mark wasn't always just one digit as you can see in the examples 2 and 3. The range operator e.g. {1,3} did not work within the lookbehind. As I found out in another stackoverflow question.
So I decided to use the or "|" operator as sugested here:
|[0-9]{2} //Or followed by two digits between 0 and 9
)
The interesting part is that it also matches the comma in the third example "123,23". I don't really understand why. Also I don't know why I don't have to add the starting quotation mark after the or "|" operator again, because I thought that the complete lookbehind until the or operator would be necessary to be modified or repeated e.g.:
(?<=\"[0-9]|\"[0-9]{2})(,)(?=[0-9]{2}\") //This however does not work at all
So in my understanding the corresponding regular expression to match all three examples should look like the following:
(?<=\"[0-9]|\"[0-9]{2}|\"[0-9]{3})(,)(?=[0-9]{2}\")
or at least (if someone can explain the missing \"):
(?<=\"[0-9]|[0-9]{2}|[0-9]{3})(,)(?=[0-9]{2}\")
I hope someone is able to help me understand the situation.
//Edit: If it is of special interest, I used this regex in a regular textfile in the sublime text 3 editor, to search for the comma and replace it.