Home > Back-end >  Regex - starts with one of list of characters and has length between 1 and 3 symbols, ending with wh
Regex - starts with one of list of characters and has length between 1 and 3 symbols, ending with wh

Time:09-05

I am cleaning data where I want to detect all characters starting with 'i' or 'f' and with length between 1 and 3.

Example (ignore the meaning of the text):

  • payment for i date
  • payment for inv date
  • payment for f

I need to catch the bold string.

This is what I have so far but it is not getting the long string inv

\s[i|f]{1}|\s[i|f]{2}|\s[i|f]{3}

CodePudding user response:

You can use this simple regex:

\b[if]\p{L}{0,2}\b

Or if you want ignore case matches then use:

\b[ifIF]\p{L}{0,2}\b

RegEx Demo

RegEx Breakup:

  • \b: Word boundary
  • [if]: Match i or f
  • [ifIF]: Match i or f or I or F
  • \p{L}{0,2}: Match 0 to 2 unicode letters
  • \b: Word boundary
  • Related