Regex - case sensitivity when replacing

I am trying to reduce the number of regex comparisons by doing case sensitive registration based on the source pattern.

For example, if I wanted to check for cl, co, ca,

    pattern = "c([loau])" - replace with "k$1"

      

However, do the same with the uppercase "C", I am duplicating the test using capital C in the search, because the replacement must start with "K"

    pattern = "c([loau])" - replace with "k$1"

      

I could of course find all occurrences with either:

    pattern = "[Cc]([loau])"

      

The problem is that then I am not sure in which case the replacement should be (upper or lower case "k"). What form of the replacement string will be selectively replaced based on the case of a matching pattern?

+3


source to share


1 answer


Let's say you are using a template like this:

pattern = (?<prefix>.*)(?<toChange>[Cc])(?<rest>[loau])(?<suffix>.*)

      

You can simply create a new line consisting of (pseudocode):



replacementString = ${prefix} + changedCharConvertedToString + ${rest} + ${suffix}

      

where changedCharConvertedToString

is the captured group ${toChange}

converted to char

, with 8 appended and converted back to string. This is because ASCII characters are assigned the corresponding decimal values, and the distance between the same small and uppercase letters is always the same. If you add 8 to char 'C'

you get 'K'

. Similar to the small one 'C'

you get 'K'

. Google for "ASCII table" if you need other letter combinations.

+1


source







All Articles