Reputation: 6039
I want to find all occurrences of .
in a string that occur before a =
. The string that I am performing the search on is:
case pencil.tip.crop.circle.badge.plus = "pencil.tip.crop.circle.badge.plus"
I am using the following regular expression to target everything that occurs before the =
:
.+?(?==)
Below, you can see the results of the search:
The highlighted result is from where I would like to extract all occurrences of .
in order to obtain all occurrences of .
that occur before the =
.
I assume that I need to escape .
in my expression, but I do not know where I need to do that in order to extract all occurrences. I understand that .+?
matches all characters, but replacing .+?
with \.+?
yields no results:
What expression do I need in order to obtain all occurrences of .
that occur before the =
?
Upvotes: 2
Views: 2059
Reputation: 18611
.+?(?==)
- matches one or more non-linebreak characters as few as possible, that are are followed with =
. It happens because the dot is not escaped, and the (?==)
lookahead looks immediately to the right.
I am afraid \.(?=.*=)
from this answer may overmatch if a =
character appears in the double quoted value.
There are at least two more precise alternatives:
\G[^\r\n.=]*\K\.
See proof (PCRE). Matches zero or more characters other than linebreaks, dots and equal signs, dropped later with \K
, and \.
matches literal dots.
Also, consider
(?<=^[^\r\n=]*?)\.
See proof (JS). Matches any dot that has zero or more characters other than linebreaks and equal signs, as few as possible, at the start of string before the dots.
Upvotes: 1