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

Time:04-12

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

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.

CodePudding user response:

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
}}

  • Related