regex find characters between “ ”

后端 未结 4 1962
不知归路
不知归路 2020-12-13 18:52

How can I match all characters between 2 specified characters, say \" \" -> from sdfsf \" 12asdf \" sdf

I want to get 12asdf o

相关标签:
4条回答
  • 2020-12-13 19:34

    I suggest you use

    (?<=")(?:\\.|[^"\\])*(?=")
    

    This will match only what is between the quotes (not the quotes themselves) and also handle escaped quotes inside your string correctly.

    So in "She said, \"Hi!\"", it will match She said, \"Hi!\".

    If you're using JavaScript or Ruby (which you didn't mention) and therefore can't use lookbehind, use

    "((?:\\.|[^"\\])*)"
    

    and work with the capturing group no. 1.

    0 讨论(0)
  • 2020-12-13 19:41
    [^"].*[^"]
    

    If you enter: "Elie", it will give Elie (note: without quotes)

    0 讨论(0)
  • 2020-12-13 19:48

    You can use the following pattern to get everything between " ", including the leading and trailing white spaces:

    "(.*?)"
    

    or

    "([^"]*)"
    

    If you want to capture everything between the " " excluding the leading and trailing white spaces you can do:

    "\s*(.*?)\s*"
    

    or

    "\s*([^"]*)\s*"
    
    0 讨论(0)
  • 2020-12-13 19:49

    You can use preg_match(/"([^"]*)"/,$input,$matches);. $matches[1] will have what you want.

    0 讨论(0)
提交回复
热议问题