Regex to match the specified text, then nothing but the specified text, then nothing, then the specified text

I've done a ton of searching but I think this is just over my head. I cannot figure out what should I do to parse this text.

Let's say I have lines: case 1: "hello, hello, how are you today" case 2: "hello, hello, how are you today" case 3: "hello, hello, doing you today" case 4: "hello, hello how are you doing today blah "

If I wanted to match where any text is, then "hello", then any text (but not "do" and ends with "today", how do I do that?

On http://www.regexpal.com/ I am using (hello)((?!, doing).*)

that will not select case 3 but will select cases 2 and 4 where I want it to only select case 1. Any thoughts?

+3


source to share


1 answer


you can use

^.*?hello(?:(?!,\s+doing).)*today$

      

See regex demo



More details

  • ^

    - beginning of line
  • .*?

    - any 0+ characters other than line break characters, as few as possible
  • hello

    - literal substring
  • (?:(?!,\s+doing).)*

    - zero or more non-line break characters, as many as possible, that do not start a sequence, defined as:
    • ,

      - comma
    • \s+

      - spaces 1+
    • doing

      - literal substring
  • today

    - literal substring
  • $

    - end of line.
+1


source







All Articles