Home > Software design >  Write regex pattern in java to work for multiple && condition
Write regex pattern in java to work for multiple && condition

Time:03-01

This is pattern ^\w \.\w *= *. $ that works for parent.child1=value11. This is pattern ^\w \.\w *= *. *&& *\w \.\w *= *. $ that works for this parent.child1=value1 && parent.child2=value2. Is there an easier way to write a RegEx pattern that will work for both? Even more to work for multiple && condition(cond1 && cond2 && cond3...)?

CodePudding user response:

You may use this regex to match single ot multiple conditionals:

^\w \.\w  *= *\S (?: *&& *\w \.\w  *= *\S )*$

RegEx Demo

Repetition in (?: *&& *\w \.\w *= \S )* will let me match 0 or more occurrences of your conditions thus allowing it to match all of your desired expressions.

  • Related