Search code examples
regexstringjflex

Trying to match up strings in ""


"[a-zA-Z0-9]*"

which I wish to match the pattern of the form

"testing" 

But my regex is not matching any of these strings. Where I'm making the mistake?

Thanks in advance.


Solution

  • if your requirement is "matching" not "extracting":

    ^"[a-zA-Z0-9"]*"$ would be the regex you need.
    

    note that since your 2nd example has nested double quotes, you need " in your []

    test with grep:

    kent$  echo '"testing"
    "testing123"hello""
    '|grep -E '^"[a-zA-Z0-9"]*"$'
    "testing"
    "testing123"hello""
    

    if you want to extract things between double quotes, you need to explain, what do you want to get in your 2nd example.

    EDIT

    if you just want to extract the things between double quote:

    (?<=")[^"]* 
    

    is what you are looking for.

    still testing with grep:

    kent$  echo '"xxx bbb _ foo bar"'|grep -Po '(?<=")[^"]*'
    xxx bbb _ foo bar