Simple regex required

I've never used regex in my life and it seemed to look like a deep pool to dive into. Anyway, I need a regex for this pattern (AN is alphanumeric (az or 0-9), N is numeric (0-9), and A is alphabetic (az)):

AN,AN,AN,AN,AN,N,N,N,N,N,N,AN,AN,AN,A,A

      

It's five ANs, followed by six Ns, and then three ANs, and then finally two A.If it matters, the language I'm using is Java.

0


a source to share


6 answers


[a-z0-9]{5}[0-9]{6}[a-z0-9]{3}[a-z]{2}

      



should work in most RE dialects for tasks as you pointed it out - most will also support abbreviations such as \d

(digit) instead [0-9]

(but if alphabets are supposed to be lowercase seem to be asking, you will probably need to specify parts a-z

) ...

+7


a source


Replace every AN with [a-z0-9], every N with [0-9], and every A with [az].



+4


a source


30 seconds in Expresso :

[a-zA-Z0-9]{5}[0-9]{6}[a-zA-Z0-9]{3}[0-9]{2}

      

Case insensitive, but you can probably define this in Java instead of regex.

+1


a source


For the example you posted, the following should work fine.

(([A-Za-z\d])*,){5}+(([\d])*,){6}+(([A-Za-z\d])*,){3}+([\d])*,[\d]*

      

In Java, you can use it like this:

boolean foundMatch = subjectString.matches("(([A-Za-z\\d])*,){5}+(([\\d])*,){6}+(([A-Za-z\\d])*,){3}+([\\d])*,[\\d]*");

      

I used this tool to help you learn RegEx and it's very easy to do. http://www.regexbuddy.com/

+1


a source


Try looking into some simple java regex tutorials like this

They will tell you how you form regular expressions as well as how to use it in java.

0


a source


This should match the pattern you are asking for.

[a-z0-9]{5}[0-9]{6}[a-z0-9]{3}[a-z]{2}

      

In addition, you can add "Start line / line end matches" matches if the line match should fail if there are other characters in it:

^[a-z0-9]{5}[0-9]{6}[a-z0-9]{3}[a-z]{2}$

      

0


a source







All Articles