简体   繁体   中英

Get text between quotation marks

I have a text like this "my text" "my text2" . How can I extract my text and my text2 into an array?

Loop over your input with:

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

and capture first group.

This construct will handle embedded quotes as well as empty strings. If you do not want empty strings, replace the first * with a + .

With PHP, this is done using preg_match_all :

preg_match_all('/"([^"]*(?:\\"[^"]*)*)"/', "input here", $matches);
# use the $matches array

If your strings don't have embedded quotes, you could simply use "(.*?)" . If they do, then it depends on how you quote them, but "(([^"\\\\]|\\\\.)*)" would handle backslash-quoted strings such as "\\"\\\\" . (Note: None of these strings are themselves quoted, except for the backslashes in the second regexp.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM