Remove specific string using RegExp

I have a different string:

Word1-A-OK
Word2-Sample-R-OK
Word3-A

How can remove these (-A, -A-OK, -A-NG, -R, -R-OK, -R-NG, -W, -C) strings using Regexp? The output I would want is:

Word1
Word2-Sample
Word3

This is my current RegExp:

(\\.|-A|[^\w]+R|[^\w]+OK|[^\w]+NG|[^\w]+W|[^\w]+C)  

2 answers

  • answered 2020-07-29 17:23 The fourth bird

    You could match for the example data

    -[AR](?:-OK)?
    

    And replace with an empty string

    Regex demo

    To match all of the combinations

    (\w+)-(?:[AR](?:-(?:NG|OK))?|[WC])$
    

    Regex demo

    And replace with group 1.

  • answered 2020-07-29 17:23 Tim Biegeleisen

    You seem to be overcomplicating this. Just do a regex replacement on the following alternation:

    -(?:[AR](?:-(?:OK|NG))?|W|C)$
    

    Then, replace with empty string to effectively remove these suffixes.

    Demo