An example of how the Strings may look:
TADE000177
TADE007,daFG
TADE0277 DFDFG
Thank you!
An example of how the Strings may look:
TADE000177
TADE007,daFG
TADE0277 DFDFG
Thank you!
It's a little unclear what you want.
If you mean four capital letters from A to Z, followed by at least one digit in 0-9 you could try this:
"^[A-Z]{4}[0-9]+"
[A-Z]
to .
.+
to a *
."^\w{4}\d*$"
That should match any 4 characters and any number of digits. How ever, according to your example, there might be characters following the digits. Maybe if you give us a sample input and a sample output we might be able to help more.
For more info about regex, check here
If i understood correctly what you're asking for you can try: .{4}\d*
The following...
import java.util.regex.*;
public class Test {
public static void main(String[] args) {
Pattern p = Pattern.compile("^\\p{Alpha}{4}\\d+");
String[] tests = { "TADE000177", "TADE007,daFG", "TADE0277 DFDFG",
"TADE", "TADE0", "TADEX456" };
for (String test : tests) {
Matcher m = p.matcher(test);
System.out.print("Input: " + test+ "... ");
if (m.find())
System.out.println("Found match: " + m.group(0));
else
System.out.println("No match.");
}
}
}
...prints
Input: TADE000177... Found match: TADE000177
Input: TADE007,daFG... Found match: TADE007
Input: TADE0277 DFDFG... Found match: TADE0277
Input: TADE... No match.
Input: TADE0... Found match: TADE0
Input: TADEX456... No match.
By removing the ^
from the pattern, the last test would also match on the string ADEX456
.
^\w{4}.*$
Matches a string starting with 4 characters followed by any number of any other charcters. Your examples include spaces and punctuation, if you know exactly which characters are allowed then you might want to use this pattern.
^\w{4}[A-z\d<other known characters go here>]*$
Remember to remove the < and > too :)