Complete or Partial regexp matching
Hi
I have a searching process where a user can enter a partial search string or the complete string but it must always match the pattern ABC/AA99/99999.
For example
ABC
ABC/
ABC/AA and
ABC/AU02/12345
are all valid searches and should be be passed by the expression.
I have tried a few different methods but when I start to loosen things off it allows search value like ABC//A to to pass !
The complete pattern match is: "^ABC/[A-Z][A-Z]/\\d{0,6}$"
I have tried different variation of this to match partials with varying success. Do I need to use grouping or | seperator and spell all the possibilities out?
Can someone please help? I am use java 1.4
Cheers
Chris
[751 byte] By [
ChrisO2a] at [2007-11-27 3:47:38]

Why are you using regexp? Why not check String.startsWith(String prefix)?Kaj
Something like this?
String regex = "ABC[(?<=/)\\w]*?[(?<=/)\\d]{0,6}";
String[] tests = {"ABC",
"ABC/",
"ABC/AA",
"ABC/AU02/12345"};
for(int i = 0; i < tests.length; i++) {
if(tests[i].matches(regex)) {
System.out.println(tests[i]+" OK.");
} else {
System.out.println(tests[i]+" wrong.");
}
}
I think you need
Pattern p = Pattern.compile("ABC/[A-Z][A-Z]\\d\\d/\\d{0,6}");
String[] goodData =
{
"ABC",
"ABC/",
"ABC/AA",
"ABC/AU02/12345",
};
for (String goodValue : goodData)
{
Matcher m = p.matcher(goodValue);
boolean isGood = m.matches() || m.hitEnd();
System.out.println(isGood + "\t" + goodValue);
}
String[] badData =
{
"ABC//",
"ABC/AU02/1234568",
"ABC/AU021/1234",
};
for (String badValue : badData)
{
Matcher m = p.matcher(badValue);
boolean isGood = m.matches() || m.hitEnd();
System.out.println(isGood + "\t" + badValue);
}
...>Matcher m = p.matcher(goodValue);>boolean isGood = m.matches() || m.hitEnd();>System.out.println(isGood + "\t" + goodValue);...Thanks but this wont work as I am confined to java 1.4 and hitEnd() is in Java 5
> Why are you using regexp? Why not check
> String.startsWith(String prefix)?
>
> Kaj
This would be useful if my Pattern was well defined but I don't see how this would work when the prefix is variable? ie ABC is always the same but then a slash and any alpha numeric values can follow.
I would need quite a few tests for partials etc?!
> String regex =
> "ABC[(?<=/)\\w]*?[(?<=/)\\d]{0,6}";
> String[] tests = {"ABC",
>"ABC/",
> "ABC/AA",
>"ABC/AU02/12345"};
> = 0; i < tests.length; i++) {
> if(tests[i].matches(regex)) {
>System.out.println(tests[i]+" OK.");
> } else {
> System.out.println(tests[i]+" wrong.");
>}
>}
While this is the sort of thing I am after it still allows invalid Strings to pass on partial matches
ie
If you use ABC//A it passes but the pattern should only allow 1 slash.
If you use ABC/123 it passes but the pattern should enforce that 2 alpha characters follow ABC/.
The full valid string is ABC/AA99/999999 it should match this pattern exactly from start to finish but at any level ie 1 character A, or several ABC/AU02/.
As I think you have also found finding a pattern that works for expected Strings is not too bad but excluding bad Strings is proving rather difficult.
I appreciate you efforts though. ta
Wish I could use Java 5!
I almost have it sorted now. I found a project called JRegex which does Incomplete matching and works a treat.
I only have one expected match that fails ABC/ZA0 should pass but it fails because I have defined that part of the pattern as \\d{2,4} to ensure that at least 2 digit are entered before the next / is allowed. Trouble is if the user enters 1 digit it fails.
The full expression I have is ABC/[A-Z][A-Z]\\d{2,4}/\\d{0,6}.
Once again help would be appreciated.
thanks
Chris
> ...
>
> The full expression I have is
> ABC/[A-Z][A-Z]\\d{2,4}/\\d{0,6}.
That will only match the "full" Strings, not the short versions like "ABC/AA", "ABC/AA1", "ABC" ...
Still not quite sure what exactly is a good String, and what a bad String is.
Try this:
public class Main {
public static void main(String[] args) {
String regex = "ABC/?((?<=ABC/)[A-Z]{2}\\d{0,2})?"+
"/?((?<=/[A-Z]{2}\\d{0,2}/)\\d{0,6})?";
String[] good = {"ABC",
"ABC/",
"ABC/AA",
"ABC/AA2",
"ABC/AA22",
"ABC/AA22/122",
"ABC/AA22/12345"};
for(int i = 0; i < good.length; i++) {
test(good[i], regex);
}
System.out.println();
String[] wrong = {"ABC/1",
"ABC/A1",
"ABC/AA211",
"ABC/AA22/A",
"ABC/A22/1234",
"ABC/AA22/1234567"};
for(int i = 0; i < wrong.length; i++) {
test(wrong[i], regex);
}
}
private static void test(String s, String regex) {
if(s.matches(regex)) {
System.out.println(s+" = OK");
} else {
System.out.println(s+" = wrong");
}
}
}
In JRegex, what happens if you use this regex? "ABC/[A-Z][A-Z]\\d\\d\\d{0,2}/\\d{0,6}"