简体   繁体   中英

How to make a regex pattern that fits

I'm trying to learn regex and came across a question I cannot solve. The pattern looks like this:

A1234567 4 DFDGB

B1234567 1234 DFDRR

C1234567 12 DBFDG

The parts I want are the highlited ones. 1 letter followed by 7 numbers then a space and less than 5 numbers.

Thank you in advance!

If I understood correctly, then this is what you need

[A-Z]{1}\d{7}\s\d{1,5}

View details:

  • [AZ]{1} - 1 letter from the AZ range
  • \\d{7} - 7 digits
  • \\s - space
  • \\d{1,5} - numbers range from 1 to 5
(\w{1}([\d ]+))

This Will Work

\\w{1} - Will match 1 Letter

([\\d ]+)) - Will match any digits and spaces or ([\\d ]{1,6}) Which will only match up to 5

Example here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM