0

I seem to have hit my regex knowledge limit and my google-fu is failing me.

I try to not-match strings, with preg_match that either start with one or more slashes or (and there it gets tricky for me) with ((

My original which was used to just match / looked like that:

\^[^\/].*$\

And it worked beautifully. However when I try to match two brackets on the beginning of the string I fail. I would post all my approaches I tried but I don't think adding them will clarify.

Best I could do was simply adding the bracket into the character class which kind of works but already with one bracket:

\^[^\/(].*$\

Examples:

String: Hello

Desired result: Match

String: /Hello

Desired result: No Match

String: //Hello

Desired result: No Match

String: (Hello

Desired result: Match

String: ((Hello

Desired result: No Match

I really hope you could give me a push into the right direction.

Thanks

4

2 回答 2

1

Well, it's possible to build the pattern exactly the way you seem to look for with lookaheads:

  $testStrings = array(
    'Hello', '/Hello', '//Hello', '(Hello', '((Hello'
  );

  foreach ($testStrings as $test) {
    echo $test . ' is ' .  
      ( preg_match('#^(?!/{1,2})(?!\(\()#', $test ) ? ' matched' : 'not matched ' ) 
      . '<br/>';
  }
  /** prints...
Hello is matched
/Hello is not matched
//Hello is not matched
(Hello is matched
((Hello is not matched 
  */

The point is that you 'neg-check' for two cases - either one or two forslashes OR at least two parenthesis - right after the string beginning anchor.

I don't know, though, what should be done with '///Hello' and '(((Hello' strings, whether you intend them to not-match as well - or not.

于 2012-08-27T16:53:14.353 回答
0

Check this:

\^[^/(]{2}.*$\ 

maybe?

于 2012-08-27T16:47:33.173 回答