简体   繁体   中英

Regex is matching odd spaces

I need to match roman numerals in text. For example Star Wars VII The Force Awakens I use this expression (?:^|\\s)(IX|IV|V?I{0,3})(?:\\s|$) but it matches VII with spaces, ie _VII_

I tryed (?<=^|\\s)(IX|IV|V?I{0,3})(?=\\s|$) but get exeption error: look-behind requires fixed-width pattern

I think the problem is with non-capturing groups. If i use match or search - it returns only number, but with sub - it replace both non-capturing groups and capturing group (number with spaces).

The goal is to match roman numerals in text with space or nothing (start/end of line) before/after and not as part of word.

Move the alternation ( | ) outside the lookbehind:

(?:^|(?<=\s))(IX|IV|V?I{0,3})(?=\s|$)

See it in action


Alternatively, you can just replace the lookarounds with word boundaries :

 \\b(IX|IV|V?I{0,3})\\b 

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