Home > Blockchain >  Regex matching for or sembol
Regex matching for or sembol

Time:02-07

Hello I have a regex and its (^|\W)(t)|(t\W*year)($|\W)

my test case is

has to match

t-year
t

doesnt match

test123

but the problem for test123its matching it shouldn't match and also for t-year it is matching but only for t character You can check from here https://regexr.com/6eqfi

CodePudding user response:

You might for example use

\bt(?:[^\w\s]year)?\b
  • \b A word boundary to prevent a partial word match
  • t Match literally
  • (?:[^\w\s]year)? Optionally match a non word character except for a whitespace char
  • \b A word boundary

Regex demo

  •  Tags:  
  • Related