Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can you help me to find a regex for a specifc String?

So I want to check if a String matches a regex.

The String is "TU-266F".

The pattern is Letter Letter – Number Number Number Letter

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How would a regex look, that matches this String and returns true.

String s = "TU-266F";

if(s.matches[]){
return true;
}

I have to do a lot of testing, and if you could help me with this specific String I can figure out the rest alone, since they are all very similar.

>Solution :

If the pattern as you say is: ‘Letter Letter – Number Number Number Letter’ then you can use : ^[A-Z]{2}-[0-9]{3}[A-Z]$

import java.util.regex.*;
public class RegexExample1{
public static void main(String args[]){

boolean b=Pattern.compile("^[A-Z]{2}-[0-9]{3}[A-Z]$").matcher("TU-266F").matches();

System.out.println(b); // true
}}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading