regex - How do you map input to multiple replacement values with a regular expression for C# -


i have 2 input values:

apple

carrot

i find regular expression can use c# program using regex.replace() recognize these inputs , replace them unrelated values. not have chance change code. can provide input, pattern, , replacement parameters.

for example:

if apple found, replace fruit

if carrot found, replace vegetable

i can match them ^apple|tomato$ see no way make substitution want. options replacement seem input or singular fixed value.

there is, unfortunately, no way single regex replacement. there no way alter value of capturing group in pattern itself, or "hard-code" value of group in pattern.

if, however, application allows provide multiple pattern/replacement pairs executed in succession, providing pairs:

  • pair 1
    • pattern: ^apple$
    • replacement: fruit
  • pair 2
    • pattern: ^tomato$
    • replacement: vegetable

Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -