Search code examples
regexstring-search

Regex expression for any times any character between two set of literals


Hi a am new to regex and programming. I in a textual file want to search any thing (all characters) between first occurrences of two literal namely- 'html' and 'http'. I have tried lot of expression, but no success. Any help will be appreciated.


Solution

  • You could try this regex,

    (?<=html).*?(?=http)
    

    Use s switch to make dot to match newlines also.

    Explanation:

    • (?<=html) Positive lookbehind is used. It matches all the characters after the word html.
    • .*? It matches any character zero or more times. ? after * makes the regex engine to match the shortest possibility.
    • (?=http) Positive lookahead. Matches any characters before http.