2

I have strings of a particular pattern as below which i need to validate:

String a ="test/<value>"
String b = "test/test1/<value>"
String c = "test3";
String d = "test4"

where value - can be any thing i.e alphabets,numeric,symbol,spl characters.

String c and String d are pretty straight forward.The issue comes for string a,b.

Valid Strings:

test/12 | test/abc | test/12.39 ...
test/test1/12 | test/test1/abc | test/test1/12.30
test3
test4

Invalid Strings:

test1/12
test/test123/12

test,test1,test3,test4 are key words. any change in that should return me false.

I tried with the below regex pattern:

Pattern  pattern1 = "^(test/(test1/)?[A-Za-z0-9]+|test3|test4)?/?$"

it works fine for few scenario's i.e

test/123 (pass)
test/test1/abc123(pass)

It fails when it has any symbol,decimal value or spl characters:

test/10.12 (Fail)
test/test1/@#$$ (Fail)

Pattern pattern2 = "^(test/(test1/)?[A-Za-z0-9/*!@#$%^&*()\"{}_|\\?/<>,.]+|test3|test4)?/?$";

When i use the above pattern if there are any change in the keyword it fails.

test/test1344/123(Fail)

i.e even if i change the keyword it returns as true.Expected result is false.

Please advice as to how i can validate the above strings ?

2
  • 1
    I think a tokenizer is more appropriate in your case, as it looks like you're using slash delimited tokens, and each token determines the valid values of the next one. Commented Sep 4, 2013 at 8:55
  • 1
    Do i understand correctly that your problem is that the second part can be anything but a variation of test1? Like, you can have test/foo, but not test/test12? Commented Sep 4, 2013 at 8:58

2 Answers 2

1

You can use this regex:

^(?:test/(?:test1/)?[^/]+|test3|test4)/?$

I just replaced [A-Za-z0-9] by [^/] (any non forward slash character)

Sign up to request clarification or add additional context in comments.

6 Comments

I think the problem is that he wants to also forbid things like test/test1344/123, while allowing test/test2FOOOO
The keywords(test,test/test1,test3,test4) are static and what ever is after keyword can have any value.
@anubhava. Thanks it works. It would be great it you can explain the expression.
Most of the regex was taken from your question. I am using [^/] which means match anything except a forward slash. More importantly I removed ? just before closing /?$ from your regex otherwise regex could even match an empty string or just /. Finally I used ?: for non capturing group (not important).
Just a side note: Depending on what you want, this might not be enough, as it still allows test/test123
|
0

Try ^(test3|test4|test(/test1)?/([0-9.]+|[A-Za-z]+))$/ .

2 Comments

it fails when the value is "test/123" String test= "^(test3|test4|test(/test1)?/([0-9.]+|[A-Za-z]+))$/"; String name1="test/12"; final Pattern p = Pattern.compile(test); final boolean isValid = p.matcher(name1).matches(); have i missed something ?
No, I wrongly added a trailing slash. Remove that and it should work. Also, try one of the many online regex building tools, such as txt2re.com

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.