Home > front end >  how to create a regex that starts with a specific letter and ends with x amount of numbers
how to create a regex that starts with a specific letter and ends with x amount of numbers

Time:10-06

I'm trying to create a particular regex, and it wouldn't come out. The idea is to build a regex that starts with the letter 'a', 'b', or 'c' and is followed by five random numbers. You can't have anything ahead or behind. The idea is to scrape GitHub files looking for this particular pattern

valid examples:

1) a454744 = a454744
2) lalala: a787878 = a787878
3) b121351 lalala = b121351
4) lalala:c454545

Invalid examples:

1) aa454744
2) a1234567
3) c454545lalala

What I have

\b[abc]\d{5}$|^[abc]\d{5}$

CodePudding user response:

^[abc]\d{5}$

  • ^: beginning of string
  • [abc]: match "a", "b", or "c"
  • \d: digit
  • {5}: match previous smybol 5 times
  • $: end of string

CodePudding user response:

Try this regex pattern. It matches all the valid cases in your description and does not match the invalid cases in your description.

\b[abc]\d{5,6}\b

  • \b assert position at a word boundary
  • [abc] match a single character in the list (case sensitive)
  • \d{5,6} match a digit [0-9] Between 5 and 6 times, as many times as possible
  • Related