I would like a regex to match all characters after a certain word appear, and stop matching after if this same word appears (or if the expression ends). For example if I want to match every character after "by" or "per" and stop when "by" or "per" appears again:
So far, I have done the following:
(by|per)\s(\w+)
But only gives me one word
You may use
\b(by|per)\s+(.*?)(?=\s*(?:\b(?:by|per)\b|$))
See the regex demo
Details
\\b
- a word boundary (by|per)
- Group 1: by
or per
words \\s+
- 1+ whitespaces (.*?)
- Group 2: any zero or more chars other than line break chars, as few as possible up to the first occurrence of... (?=\\s*(?:\\b(?:by|per)\\b|$))
- a sequence of
\\s*
- 0+ whitespaces (?:\\b(?:by|per)\\b|$)
- either of
\\b(?:by|per)\\b
- by
or per
whole words |
- or $
- end of string.
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.