How to make a Regex that specifies elements in any order? -
i want regex matches string contains
- @ least 1 brace: } or { , - @ least 1 digit: \d , - @ least 1 instance of either: <p> or </p> but in order, following matched:
<p>{123 2}</p> 2<p>}}} {}{}{}<p></p></p>234234}}} and none of these matched:
<p>{ alphabet 123 {2} {{{}}} <p>1</p> here's have far, demands 1 of of components:
(<\/p>|<p>|\d|\}|\{)+ my problem don't know how make more general without having specify order this:
(<\/p>|<p>)+(\d)+(\}|\{)+ or making stupidly long enumerate every possible order...
how can "needs @ least 1 of each of these components in order?"
thanks.
if regex flavor supports lookaheads, can use positive lookahead as:
^(?=.*(\{|\}))(?=.*\d)(?=.*(<p>|<\/p>)).*$ this regex uses positive lookahead assert string atleast 1 of either { or }, @ least 1 digit , atleast 1 of either <p> or </p>.
if want ensure string has these , nothing else can use regex:
^(?=.*(\{|\}))(?=.*\d)(?=.*(<p>|<\/p>))(<\/p>|<p>|\d|\}|\{)*$ which works previous regex ensures input has no other character.
the regex can made bit shorter as:
^(?=.*[{}])(?=.*\d)(?=.*<\/?p>)(<\/?p>|[\d}{])*$ which makes use of fact \{|\} same [{}], <p>|<\/p> same <\/?p>.
Comments
Post a Comment