Regular expression to find two strings anywhere in input

后端 未结 7 529
情话喂你
情话喂你 2020-11-30 20:34

How do I write a regular expression to match two given strings, at any position in the string?

For example, if I am searching for cat and mat

7条回答
  •  旧时难觅i
    2020-11-30 20:52

    You can try:

    \bcat\b.*\bmat\b
    

    \b is an anchor and matches a word boundary. It will look for words cat and mat anywhere in the string with mat following cat. It will not match:

    Therez caterpillar on the mat.

    but will match

    The cat slept on the mat in front of the fire

    If you want to match strings which have letters cat followed by mat, you can try:

    cat.*mat
    

    This will match both the above example strings.

提交回复
热议问题