regex

Replace patterns that are inside delimiters using a regular expression call


I need to clip out all the occurances of the pattern '--' that are inside single quotes in long string (leaving intact the ones that are outside single quotes).

Is there a RegEx way of doing this? (using it with an iterator from the language is OK).

For example, starting with

"xxxx rt / $ 'dfdf--fggh-dfgdfg' ghgh- dddd -- 'dfdf' ghh-g '--ggh--' vcbcvb"

I should end up with:

"xxxx rt / $ 'dfdffggh-dfgdfg' ghgh- dddd -- 'dfdf' ghh-g 'ggh' vcbcvb"

So I am looking for a regex that could be run from the following languages as shown:

    +-------------+------------------------------------------+
    |  Language   |                  RegEx                   |
    +-------------+------------------------------------------+
    | JavaScript  |  input.replace(/someregex/g, "")         |
    | PHP         |  preg_replace('/someregex/', "", input)  |
    | Python      |  re.sub(r'someregex', "", input)         |  
    | Ruby        |  input.gsub(/someregex/, "")             |
    +-------------+------------------------------------------+

Solution

  • I found another way to do this from an answer by Greg Hewgill at Qn138522
    It is based on using this regex (adapted to contain the pattern I was looking for):

    --(?=[^\']*'([^']|'[^']*')*$)
    

    Greg explains:

    "What this does is use the non-capturing match (?=...) to check that the character x is within a quoted string. It looks for some nonquote characters up to the next quote, then looks for a sequence of either single characters or quoted groups of characters, until the end of the string. This relies on your assumption that the quotes are always balanced. This is also not very efficient."

    The usage examples would be :

    I have tested this for Ruby and it provides the desired result.