【发布时间】:2018-08-29 15:28:40
【问题描述】:
我有一个要从中解析字符串的文本文件。问题是在完全相同的文件中,字符串包含在单引号 (')、双引号 (") 或 3x 单引号 (''') 中。到目前为止我能得到的最好结果是使用这个:
((?<=["])(.*?)(?=["]))|((?<=['])(.*?)(?=[']))
只匹配单引号和双引号之间的单行字符串。请注意,文件中的字符串包含在每种类型的引号中,可以是单行或多行,并且每种类型的字符串在文件中重复多次。
这是一个示例字符串:
<thisisthefirststring
'''- This is the first line of text
- This is the second line of text
- This is the third line of text
'''
>
<thisisanotheroption
"Just a string between quotes"
>
<thisisalsopossible
'Single quotes
Multiple lines.
With blank lines in between
'
>
<lineBreaksDoubleQoutes
"This is the first sentence here
After the first sentence, comes the blank line, and then the second one."
>
【问题讨论】:
-
你能分享一个示例字符串吗?
-
抱歉,您的问题是什么?您基本上需要在您的正则表达式中添加
'''案例吗? -
另请注意,使用不情愿的量词 (
.*?) 效率不高,请改用否定字符集。有关说明,请参阅 this answer 或 this answer。 -
我们不能等待 OP 的例子吗?你怎么知道什么是有效的,什么是无效的?
-
没有示例字符串,没有语言或应用程序/工具标签(因为模式语法取决于它们),在 cmets 中没有问题的答案 = 以 太宽泛 结束问题。即使你努力建立一个模式并试图解释你的问题。
标签: regex