简体   繁体   中英

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.

(?<=\()(CAM SYSTEM:)([^)]+)(?=\))

I have tried a couple of things (?<=\()(CAM SYSTEM:)([^\s)]+)(?=\)) and (?<=\()(CAM SYSTEM:)([^\s*)]+)(?=\)) with no luck

I am expecting to have 2 groups. CAM SYSTEM: and CIMATRON E13 , current REGEX gives me 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.
  • * 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 ) .

Demo on regex101

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM