0

I have 2 string arrays, one is a list of words and the other a list of abbreviations for those words. Is there a way using REGEX that I can replace any of the words from list one that are found in the string, with the appropriate abbreviation from list two?

Const DirectionalNames As String = "(North|East|South|West|Northeast|Southeast|Northwest|Southwest)"
Const DirectionalStandards As String = "(N|E|S|W|NE|SE|NW|SW)"

MySample as string = "Go North, then take the Southeast highway for 5 miles."

Want to change it to: "Go N, then take the SE highway for 5 miles."

Thanks in Advance.

Tried various forms of replace using REGEX but couldn't find anything that worked.

  • 3
    In what language? In most languages you can use a function as the replacement, and it can look up the match in a mapping data structure. – Barmar May 18 '23 at 17:35
  • [this question](https://stackoverflow.com/questions/14156473/can-you-write-a-str-replace-using-dictionary-values-in-python) shows how to do it in Python. It will be similar in JavaScript and PHP. – Barmar May 18 '23 at 17:36
  • If you don't mind case, you could use a (language agnostic) regexp replace [llike **this demo**](https://regex101.com/r/aCNpwi/2). – bobble bubble May 18 '23 at 22:51

0 Answers0