I have this regex
(?<=\d\.\s).+(?=\s-\s)
It works great when I have a string like
3. product - sub product
The regex gives me the product (first part). If a sub-product exists it is delimited from the product by a dash surrounded with spaces ( -
).
However, there are some products which do not have sub-product. For example:
6. ComprehensiveBolt
The regex should give me comprehensiveBolt
but it does not return anything.
What update do I need to make to my regex so I can get the product regardless of the presence, or otherwise, of a sub-product?
This is one way.
Note this is only needed if your product could be a phrase.
(?<=\\d\\.\\s)(?:(?!\\s-\\s|\\d\\.\\s).)+
https://regex101.com/r/uC2yDs/1
Partial explanation
(?<= \d \. \s ) # This must be behind
(?: # -----------
(?! # Neg assertion
\s - \s # Not this ahead
| \d \. \s # Nor this ahead
)
. # Ok, grab this character
)+ # 1 to many times
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.