简体   繁体   中英

Why this Regex selects parenthesis and after though I use look ahead

I use this regex

/\.(.+)?(?=(\(?)|\r\n)/gi

with

part1.part2
part1.part2(part3) part4

I want only match.part2 in both cases

but in second case I get.part2(part3) part4

在此处输入图像描述

You should make the .+ part non-greedy, by using .+? , as otherwise it will also capture the opening parenthesis you want to see in the look-ahead part.

Also, in the second part, don't make the \( optional, otherwise you will be OK in having nothing in your look-ahead to match.

Finally, don't match \r\n , but the end-of-line anchor $ in combination with the m flag (so that it matches the end of each line instead of the whole input).

So:

\.(.+?)(?=\(|$)

regex101 link

You see the parenthesis in the match as the . can also match ( .

The pattern will match the rest of the line after the first dot without backtracking to a ( as the parenthesis in the lookahead is optional \(? and the assertion will be true.

You could make use of a negated character class not crossing parenthesis or a newline when matching.

\.([^()\r\n]+)

Regex demo

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