[英]regex: Match everything starting from A until, but not including, B
I have a file with an input similar to this: 我有一个输入类似于此的文件:
CQUAD4 123123 123 234
CQUAD4 123123 123 234
CQUAD4 123123 123 234
PCOMP 123 123 123 123
123 123 123 123 123
123 123 1231 23
CQUAD4 123123 123 234
CQUAD4 123123 123 234
CQUAD4 123123 123 234
CQUAD4 123123 123 234
CQUAD4 123123 123 234
PCOMP 123 123 123 123
123 123 123 123 123
123 123 1231 23
432 234 2342 34
CQUAD4 123123 123 234
CQUAD4 123123 123 234
CQUAD4 123123 123 234
CQUAD4 123123 123 234
I want to capture 我想抓住
PCOMP 123 123 123 123
123 123 123 123 123
123 123 1231 23
and 和
PCOMP 123 123 123 123
123 123 123 123 123
123 123 1231 23
432 234 2342 34
I currently have PCOMP((.|\\n)*)CQUAD4
but it captures everything, including the CQUAD4
block in between. 我目前有PCOMP((.|\\n)*)CQUAD4
但它捕获所有内容,包括CQUAD4
块。 What should I use in order to match starting from PCOMP
, up until the character before the next instance of CQUAD4
? 我应该使用什么来匹配从PCOMP
开始,直到下一个CQUAD4
实例之前的字符? Thanks in advance. 提前致谢。
You might get along with 你可能相处得很好
PCOMP[\s\S]*?(?=CQUAD)
See a demo on regex101.com . 请参阅regex101.com上的演示 。
(.|\\n)
is one of the most inefficient patterns you can think of, really. (.|\\n)
是你能想到的最低效的模式之一,真的。
Use the *?
使用*?
quantifier to lazily match the inside part: 量词与懒惰的内部相匹配:
PCOMP((.|\n)*?)CQUAD4
By default, *
is greedy, and will find the biggest match possible. 默认情况下, *
是贪婪的,并且会找到最大的匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.