[英]REGEX drop leading and trailing spaces within ( )
I have a string that I am trying to extract data from我有一个字符串,我试图从中提取数据
(CAM SYSTEM: CIMATRON E13 )
I have the following REGEX that seems to get me close, but I don't want the leading spaces or trailing spaces.我有以下 REGEX 似乎让我很接近,但我不想要前导空格或尾随空格。
(?<=\()(CAM SYSTEM:)([^)]+)(?=\))
I have tried a couple of things (?<=\()(CAM SYSTEM:)([^\s)]+)(?=\))
and (?<=\()(CAM SYSTEM:)([^\s*)]+)(?=\))
with no luck我尝试了几件事
(?<=\()(CAM SYSTEM:)([^\s)]+)(?=\))
和(?<=\()(CAM SYSTEM:)([^\s*)]+)(?=\))
没有运气
I am expecting to have 2 groups.我期待有2组。
CAM SYSTEM:
and CIMATRON E13
, current REGEX gives me CIMATRON E13
CAM SYSTEM:
和CIMATRON E13
,当前的 REGEX 给了我CIMATRON E13
Here's how I would modify your regex to approach this problem:以下是我将如何修改您的正则表达式以解决此问题:
\((CAM SYSTEM:) *(.+?) *\)
\(
matches a literal (
character, but it is outside of any capturing group, so it is only included in the full match. \(
匹配文字(
字符,但它在任何捕获组之外,因此它仅包含在完整匹配中。(CAM SYSTEM:)
matches the literal string CAM SYSTEM
and puts it in the first capturing group since it is within parentheses. (CAM SYSTEM:)
匹配文字字符串CAM SYSTEM
并将其放在第一个捕获组中,因为它在括号内。*
matches any number of spaces that might be present, but it is also outside of any capturing group, so you don't need to worry about this affecting your result. *
匹配可能存在的任意数量的空格,但它也在任何捕获组之外,因此您无需担心这会影响您的结果。(.+?)
creates the second capturing group, which matches one or more of any characters, but it is a lazy match, meaning it matches the fewest characters possible while still making the match work. (.+?)
创建第二个捕获组,它匹配一个或多个任意字符,但它是惰性匹配,这意味着它匹配尽可能少的字符,同时仍然使匹配工作。*\)
matches any number of spaces followed by a literal )
character, which forces the second capturing group to match up to the last parenthesis, excluding the trailing spaces. *\)
匹配后跟文字)
字符的任意数量的空格,这会强制第二个捕获组匹配最后一个括号,不包括尾随空格。 This solution works on your given test case, and it doesn't use any lookarounds, which makes it cleaner and faster in my opinion.该解决方案适用于您给定的测试用例,并且不使用任何环顾四周,在我看来,这使它更清洁、更快。
You could use this regex:你可以使用这个正则表达式:
(?<=\()(CAM SYSTEM:)\s*(.+?)\s*(?=\))
which will not capture any spaces between the :
and the start of the value, or between the end of the value and the closing )
.这不会捕获
:
和值的开头之间或值的结尾和结束之间的任何空格)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.