Java Regex: Evaluate a string against a particular pattern -
i have strings of particular pattern below need validate:
string ="test/<value>" string b = "test/test1/<value>" string c = "test3"; string d = "test4"
where value - can thing i.e alphabets,numeric,symbol,spl characters.
string c , string d pretty straight forward.the issue comes 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 key words. change in should return me false.
i tried below regex pattern:
pattern pattern1 = "^(test/(test1/)?[a-za-z0-9]+|test3|test4)?/?$"
it works fine few scenario's i.e
test/123 (pass) test/test1/abc123(pass)
it fails when has symbol,decimal value or spl characters:
test/10.12 (fail) test/test1/@#$$ (fail) pattern pattern2 = "^(test/(test1/)?[a-za-z0-9/*!@#$%^&*()\"{}_|\\?/<>,.]+|test3|test4)?/?$";
when use above pattern if there change in keyword fails.
test/test1344/123(fail)
i.e if change keyword returns true.expected result false.
please advice how can validate above strings ?
you can use regex:
^(?:test/(?:test1/)?[^/]+|test3|test4)/?$
i replaced [a-za-z0-9]
[^/]
(any non forward slash character)
Comments
Post a Comment