简体   繁体   English

正则表达式:匹配从A开始直到但不包括B的所有内容

[英]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.

相关问题 正则表达式会选择所有内容,直到下一场比赛为止,包括换行 - Regex select everything up until next match including new lines Perl 正则表达式:删除所有内容(包括换行符),直到找到匹配项 - Perl regex: remove everything (including line breaks) until a match is found 正则表达式:匹配所有内容,直到`:`或`(` - regex: match everything until `:` or `(` 提取所有内容直到找到“/”的正则表达式,从末尾开始 - Regex that extracts everything until finds "/", starting from the end 正则表达式:将一个块与模式 A 匹配,直到它遇到另一个模式 B,同时捕获包括模式 A 的重复在内的所有内容? - Regex: Matching a block with a pattern A until it hits another pattern B while capturing everything including the repeats of the pattern A? 匹配正则表达式之后的所有内容,不包括正则表达式 - match everything after regex not including regex 匹配以THIS开头的字符串,直到正则表达式 - Match string starting with THIS until THAT Regex Java / Regex-匹配所有内容,直到下次匹配 - Java/Regex - match everything until next match 正则表达式匹配直到包含或不包含 - Regex to match until either including or not including 使用正则表达式匹配并删除字符串中不超过“ =”的所有内容 - Match and remove everything up to and including “=” in string with regex
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM