Problem combining REGEX expressions
-
Hello all, I'm a regex newbie and I'm stuck with regex in PCRE/PHP. First expression, I'd like to match letters, numbers or underscore but - no space at start or end - no more than one consecutive space This works fine : ^[\w]+([_\s]{1}[a-zA-Z0-9]+)*$ Second expression - no "private" in lower or upercase ie avoid "private", "PRIVATE", "Private" ou "pRivATe" This seem to work fine : ^((?i)(?!private).(?-i))*$ I tried to combine both regex but cannot get it work ! By the way, I'm using extendsclass.com online tester. Thanks for help.
JLO
-
Hello all, I'm a regex newbie and I'm stuck with regex in PCRE/PHP. First expression, I'd like to match letters, numbers or underscore but - no space at start or end - no more than one consecutive space This works fine : ^[\w]+([_\s]{1}[a-zA-Z0-9]+)*$ Second expression - no "private" in lower or upercase ie avoid "private", "PRIVATE", "Private" ou "pRivATe" This seem to work fine : ^((?i)(?!private).(?-i))*$ I tried to combine both regex but cannot get it work ! By the way, I'm using extendsclass.com online tester. Thanks for help.
JLO
Finally after many trials, I think I found an answer : ^(?=((?i)(?!private).(?-i))*$)([\w]+([_\s]{1}[a-zA-Z0-9]+))*$ Bye
-
Finally after many trials, I think I found an answer : ^(?=((?i)(?!private).(?-i))*$)([\w]+([_\s]{1}[a-zA-Z0-9]+))*$ Bye
I didn't attempt to parse that but there seems to be several constructs in there that would make me nervous. You have two '$' and only one '^' It appears you have two optional clauses. Optional clauses without hard anchors general are always a problem because they likely make the regex engine do a lot of work. Since you already presumably have a working solution what makes you think you need to combine them into one expression? Or another way of saying that is that regexes use an iterative process to find the best solution and more complex expressions unless carefully crafted can cause unexpected problems (slowness.)