-2

i have this regex which i want to check if string do contain this structure none of the strings below match , why?

String s = "T4018.test.12"
or 
String s = "T4018.foo.32"
or 
String s = "develop"



if(s.matches("T40[0-9][0-9][.][a-zA-Z]+|develop"))
{
    //Never matches 
}

its do match :
https://regex101.com/r/CSLugb/1 UPDATE Solved it using Pattern.compile

user63898
  • 29,839
  • 85
  • 272
  • 514
  • 1
    `[0-9][0-9]` only matches two digits correct? All your `String`'s contain four or none – GBlodgett Feb 09 '19 at 19:45
  • 1
    It must match `develop`. `matches` requires a full string match and `T40[0-9][0-9][.][a-zA-Z]+` cannot match the last `.` and digits in your first two strings – Wiktor Stribiżew Feb 09 '19 at 19:45
  • The [a-z]+ part won't match what comes after: `.12` for example isn't letters. – markspace Feb 09 '19 at 19:46
  • when i test it in https://regex101.com/ i do see it matches the string's https://regex101.com/r/CSLugb/1 – user63898 Feb 09 '19 at 19:53
  • Your link even proves why it won't match, or have you missed what ".12" isn't matched in your link? – Tom Feb 09 '19 at 19:55
  • It seems to match, see [test](https://regex101.com/r/754kRy/1) – hc_dev Feb 09 '19 at 19:55
  • @GBlodgett The first two digits are part of the "T40". Only the last two digits (of the four-digit number) need to match with `[0-9][0-9]`. – Tom Feb 09 '19 at 19:56
  • Also works for me in Java: it **does** match the string "develop"; Check your test harness. – markspace Feb 09 '19 at 19:57
  • Works: `public static void main( String[] args ) { String regex = "T40[0-9][0-9][.][a-zA-Z]+|develop"; String s = "develop"; System.out.println( s.matches( regex ) ); }` – markspace Feb 09 '19 at 19:57
  • But in Java the whole string is used for matching and your RegEx does not cover the last dot and following number (’.12’ or ’.32’). See this [test of your RegEx](https://www.regexplanet.com/share/index.html?share=yyyyyw9ydar) – hc_dev Feb 09 '19 at 20:14
  • it do match "develop" but not the other string , i need 1 regex that match all options – user63898 Feb 09 '19 at 20:38
  • Well see the answers above for why. I think it's obvious that the ".12" part of your string needs to be included in the regex you gave, and it just isn't. – markspace Feb 09 '19 at 20:59

1 Answers1

-1

Maybe you can rewrite the RegEx to "T40\\d{2}\\.\\w+\\.\\d{2}|develop" for matching more exactly. See your 3 test-cases matched in Java

hc_dev
  • 8,389
  • 1
  • 26
  • 38
  • do not match : T4018.foo.32.b – user63898 Feb 09 '19 at 20:34
  • This test-case `T4018.foo.32.b` was not specified in your question. The RegEx `"T40\\d{2}\\.\\w+\\.\\d{2}(\\.\\w+)?|develop"` will also match it because _group_ of could also exist either once or never `(\\.\\w+)?` – hc_dev Feb 09 '19 at 21:02