正则表达式 - 如何查找单词和引用的短语 [英] Regular Expression - How To Find Words and Quoted Phrases
问题描述
我想说以下几点:
Guiness Harp "Holy Moses"
以便在 C# 或 VB 中获得匹配集:
So that in C# or VB get a match set of:
Guiness
Harp
Holy Moses
基本上它会在空格上分开,除非空格周围有引号,然后引号之间的那些词被认为是一个短语.
Essentially it splits on the spaces unless there are quotes around the spaces, then those words between quotes are considered a single phrase.
谢谢,凯文
推荐答案
如果您的引用字符串中没有任何(转义或双引号)引号,您可以搜索
If you don't have any (escaped or doubled) quotes inside your quoted strings, you could search for
"[^"]*"|\S+
但是,引号将成为匹配的一部分.如有必要,可以扩展正则表达式以处理带引号的字符串内的引号.
However, the quotes will be part of the match. The regex can be extended to also handle quotes inside quoted strings if necessary.
另一种(在这种情况下更可取)可能性是使用 csv 解析器.
Another (and in this case preferable) possibility would be to use a csv parser.
例如(Python):
For example (Python):
import csv
reader = csv.reader(open('test.txt'), delimiter=' ', quotechar='"')
for row in reader:
print(row)
这篇关于正则表达式 - 如何查找单词和引用的短语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!