Home > Software design >  How can I use regex to convert Uppercase text to lowercase text in combination with a look-ahead and
How can I use regex to convert Uppercase text to lowercase text in combination with a look-ahead and

Time:11-20

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

CodePudding user response:

Make sure you make the other groups non-capturing:

(?<=(?:<CONDITION NAME="))(. )(?=(?:" DATE="DATE"))

Or leave out the innen () 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.

CodePudding user response:

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

  • Related