Bob Dylan regex examples

  1. Mama or mama

    [Mm]ama

  2. Mama at start of line

    ^\W*Mama

  3. Mama

    Mama\W*$

  4. billy or bully

    b[iu]lly

  5. Hyphenated words

    `+-+”

  6. Quoted words

    "\w+"

  7. Quoted phrases

    "[^"]+"

  8. dog, dogs, singular dog

    dog, dogs, dog\b

  9. dog at end of line

    dog$      or      dog[\W+]$

  10. both girl and boy

    girl.*boy|boy.*girl

  11. start and end with “you” (…“kicks for you”)

    ^\W*[yY]ou.*[yY]ou\W*$

  12. the letters c-a-t

    cat

  13. the word “cat”

    \bcat\b

  14. words containing “cat”

    \w*cat\w*

  15. words containing “cat”, but not “cat” itself

    \w+cat\w*|\w*cat\w+

  16. word preceding cat

    \b(\w+)\b\W*cat      or      \b(\w+)\b\W*cat\b      or      \b(\w+)\b\W*cat\b(?!')

  17. word preceding dog

    (\b\w+\b)\W+dog\b

  18. word after “my”

    my\b\s+(\w+)

  19. same word twice/thrice

    \b(\w+)\b.*\b(\1)\b      or      ((?<!')\b[\w]+(?:'\w)*)\b.*\b\1\b(?!').*\b\1\b(?!')