0

In the following example text I am trying to write a regex to match token, password , … but not items. I tried to use \'(<?items)(.*)\' => \[ without any success. What is the correct RegEx to do that?

    [
        'items' => [
            'token' => [
                'type' => 'hidden',
                'ng-model' => 'token'
            ],
            'password' => [
                'readonly' => $token,
                'type' => 'password',
            ],
            'newPassword' => [
                'type' => 'password'
            ],
            'confirmPassword' => [
                'type' => 'password'
            ]
        ]
    ]

Update

I wonder why .*(?!items), .*(?<!items) , (?<=itmes).* and (?!items).* do not work functionally the same in this particular situation?

PHPst
  • 14,868
  • 20
  • 85
  • 158
  • 2
    Why bother with regex when you can simply use `strpos` or something of that nature. Regex seems a bit of an overkill as long as you're matching exact words. – Andrei Nov 20 '15 at 12:31
  • https://regex101.com/r/nA6tP6/1 you're using the wrong regex. see the link. You need ?! not –  Nov 20 '15 at 12:34
  • What exactly are you trying to achieve? do you want the values that do not have item as key in this array? – Jerodev Nov 20 '15 at 12:36
  • 3
    That is a valid PHP array data structure already – so why not let PHP parse it, and then operate on the data structure, instead of doing string manipulation using regex …? – CBroe Nov 20 '15 at 12:47
  • @Terminus Thanks, that works. I also update the question. – PHPst Nov 20 '15 at 12:53
  • https://regex101.com/r/vY8aR1/1 more interesting(?) results. But yea, what CBroe said –  Nov 20 '15 at 12:53
  • No idea why my comment was removed when setting the question as a dupe. If Terminus' regex works for you, this post is a duplicate. A negative lookahead works as well here. If you are not confident in regex, avoid using it when it is redundant (as is the case here). If you want to study it, I recommend [What does the regex mean](http://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean). – Wiktor Stribiżew Nov 20 '15 at 13:08

1 Answers1

2

A negative lookahead should be applied at focus position (after ' char):

\'(?!items\')(.*)\' => \[
AndreyS Scherbakov
  • 2,390
  • 2
  • 18
  • 22