How to match string in single or double quoted using regex

前端 未结 3 1259
一生所求
一生所求 2021-01-18 16:48

I\'m trying to write a regex that matches strings as the following:

translate(\"some text here\")

and

translate(\'some text here\'

3条回答
  •  难免孤独
    2021-01-18 17:40

    You can alternate expression components using the pipe (|) like this:

    preg_match ('/translate(\("(.*?)"\)|\(\'(.*?)\'\))/', $line, $m)
    

    Edit: previous also matched translate("some text here'). This should work but you will have to escape the quotes in some languages.

提交回复
热议问题