Skip to main content
edited tags
Link
Wiktor Stribiżew
  • 626k
  • 41
  • 494
  • 608
added 117 characters in body
Source Link

I've been trying to write a regex to stictly match the following patterns - 3+, 3 months +, 3+ months, 3m+, 3 years +, 3y+, 3+ years. I've written a pattern - [0-9]{1,2}\s?(m|months?|y|years?)?\s?\+ which works for most of the cases exept the 3+ months & 3+ years here it matches just the 3+ part and not the month part. I want to use the matched string somewhere and this causes an issue. To accomodate this I tried adding another group to make the regex look like [0-9]{1,2}\s?(m|months?|y|years?)?\s?\+ (m|months?|y|years?)\s|$ but this is also matching for 6+ math. Can someone help me with what the issue is here, also can this regex be improved?

I can use multiple regex to solve different different use cases, but I wanted to achieve this using only 1 regex.

Thanks

I've been trying to write a regex to stictly match the following patterns - 3+, 3 months +, 3+ months, 3m+, 3 years +, 3y+, 3+ years. I've written a pattern - [0-9]{1,2}\s?(m|months?|y|years?)?\s?\+ which works for most of the cases exept the 3+ months & 3+ years here it matches just the 3+ part and not the month part. I want to use the matched string somewhere and this causes an issue. To accomodate this I tried adding another group to make the regex look like [0-9]{1,2}\s?(m|months?|y|years?)?\s?\+ (m|months?|y|years?)\s|$ but this is also matching for 6+ math. Can someone help me with what the issue is here, also can this regex be improved?

Thanks

I've been trying to write a regex to stictly match the following patterns - 3+, 3 months +, 3+ months, 3m+, 3 years +, 3y+, 3+ years. I've written a pattern - [0-9]{1,2}\s?(m|months?|y|years?)?\s?\+ which works for most of the cases exept the 3+ months & 3+ years here it matches just the 3+ part and not the month part. I want to use the matched string somewhere and this causes an issue. To accomodate this I tried adding another group to make the regex look like [0-9]{1,2}\s?(m|months?|y|years?)?\s?\+ (m|months?|y|years?)\s|$ but this is also matching for 6+ math. Can someone help me with what the issue is here, also can this regex be improved?

I can use multiple regex to solve different different use cases, but I wanted to achieve this using only 1 regex.

Thanks

Source Link

Regex not completely matching the desired patterns

I've been trying to write a regex to stictly match the following patterns - 3+, 3 months +, 3+ months, 3m+, 3 years +, 3y+, 3+ years. I've written a pattern - [0-9]{1,2}\s?(m|months?|y|years?)?\s?\+ which works for most of the cases exept the 3+ months & 3+ years here it matches just the 3+ part and not the month part. I want to use the matched string somewhere and this causes an issue. To accomodate this I tried adding another group to make the regex look like [0-9]{1,2}\s?(m|months?|y|years?)?\s?\+ (m|months?|y|years?)\s|$ but this is also matching for 6+ math. Can someone help me with what the issue is here, also can this regex be improved?

Thanks