How do you match a string that precedes and/or succeeds a string or character that matches a given pattern?
I couldn't find a general question on the Internet, so I figured I'd ask it here. What syntax would I use to match a string that precedes another string or character that matches a given pattern.
Example Suppose I have a string like this:
Lorem ipsum: dolor sit amet; consectator:
What regex would return "Lorem ipsum
" and "Lorem ipsum: dolor sit amet; consectator
" because they precede a :
?
What regex would return "dolor sit amet; consectator:
" because they succeed a :
?
Etc.
Solution 1:
-
^.+?(?=:)
matchesLorem ipsum
-
^.+(?=:)
matchesLorem ipsum: dolor sit amet; consectator
-
(?<=:).+
matchesdolor sit amet; consectator:
Have a play with explanation here