Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
127
rated 0 times [  132] [ 5]  / answers: 1 / hits: 181968  / 7 Years ago, wed, march 8, 2017, 12:00:00

Ok, i have a regex pattern like this /^([SW])w+([0-9]{4})$/



This pattern should match a string like SW0001 with SW-Prefix and 4 digits.



I thougth [0-9]{4} would do the job, but it also matches strings with 5 digits and so on.



Any suggestions on how to get this to work to only match strings with SW and 4 digits?


More From » regex

 Answers
55

Let's see what the regex /^([SW])w+([0-9]{4})$/ match




  1. Start with S or W since character class is used

  2. One or more alphanumeric character or underscore(w = [a-zA-Z0-9_])

  3. Four digits



This match more than just SW0001.



Use the below regex.



/^SWd{4}$/


This regex will match string that starts with SW followed by exactly four digits.


[#58626] Monday, March 6, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
arron

Total Points: 663
Total Questions: 119
Total Answers: 112

Location: Belize
Member since Mon, Jun 20, 2022
2 Years ago
;