regex - Java regular expressions for specific name\value format -


i'm not familiar yet java regular expressions. want validate string has following format:

string input = "[name1 value1];[name2 value2];[name3 value3];";  

namei , valuei strings should contain characters expect white-space.

i tried expression:

string regex = "([\\s*\\s\\s*];)*"; 

but if call matches() false string.

what's best regular expression it?

this trick:

(?:\[\w.*?\s\w.*?\];)* 

if want match 3 of these, replace * @ end {3}.

explanation:

  • (?:: start of non-capturing group

  • \[: escapes [ sign meta-character in regex. allows used matching.

  • \w.*?: lazily matches word character [a-z][a-z][0-9]_. lazy matching means attempts match character few times possible, in case meaning when stop matching once finds following \s.

  • \s: matches 1 whitespace

  • \]: see \[

  • ;: matches 1 semicolon

  • ): end of non-capturing group

  • *: matches number of contained in preceding non-capturing group.

see link demonstration


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -