Reputation: 65
In the context of an XML file, I want to use the XML tags in a positive look-behind and positive look-ahead to convert a value to lowercase.
BEFORE:
<CONDITION NAME="ABC-DEF-GHI" DATE="DATE">
AFTER:
<CONDITION NAME="abc-def-ghi" DATE="DATE">
Pattern's tried from other questions/regex wiki that don't work.
1.
FIND:
(?<=(<CONDITION NAME="))(.+)(?=(" DATE="DATE"))
REPLACE:
\L0
FIND:
(?<=(<CONDITION NAME=".*))(\w)(?=(.*" DATE="DATE"))
REPLACE:
\L$1
Using VS Code 1.62.1 MAC OS Darwin x64 19.6.0
Upvotes: 0
Views: 194
Reputation: 114651
Make sure you make the other groups non-capturing:
(?<=(?:<CONDITION NAME="))(.+)(?=(?:" DATE="DATE"))
Or leave out the inner ()
altogether:
(?<=<CONDITION NAME=")(.+)(?=" DATE="DATE")
Or use $2
as replacement. Everything between standard ()
becomes a captured group, no matter where in the expression they are.
And be careful with .+
, in this case [^"]+
is a much safer choice.
Upvotes: 0
Reputation: 163352
You don't need any capture groups if yo want to use lookarounds at the left and right side.
Instead of using .+
which is a broad match and can match too much, you can use a negated character class [^"]+
to match any character except a double quote, or you can use [\w-]+
to match 1 or more word characters or a hyphen:
(?<=<CONDITION NAME=")[^"]+(?=" DATE="DATE")
Replace with the full match using $0
\L$0
Another option is to use 2 capture groups with a single lookahead as lookarounds can be expensive, and replace with $1\L$2
(<CONDITION NAME=")([\w-]+)(?=" DATE="DATE")
Upvotes: 1
Reputation: 65
Pattern 2 works. The replace value just needs to change from \L$1 -> \L$2
Pattern 1 could also be used with \L$2 as the replace value.
This pattern works:
FIND:
(?<=(<CONDITION NAME=".*))(\w)(?=(.*" DATE="DATE"))
REPLACE:
\L$2
Upvotes: 0