0

I'm having trouble extracting some values, here is my expression:

flag:(\S+)=\&quot\;?((?:.(?!\&quot?\s+(?:\S+)=|">))+.)\&quot\;?

It is merely trying to extract values from an html encoded string.

Here is an example that works fine.

<p class="text1" target="701_text" flag:text1="This is a test">

Returns this for the second () capture, which is proper: This is a test

However a single value seems to return the first character, plus the ; of the first "

<p class="text1" target="701_text" flag:text1="T">

Returns: ;T instead of just T, which is what I need it to do.

I just need the expression modified to allow for the single values also while still returning properly for more than one value.

2 Answers 2

1

I think this is how you wanted your regex to be:

flag:(\S+)="((?:.(?!"(?:\S+)=|">))*.?)"

Note that you do not need to escape that much. & and ; are no special characters and as such don’t need to be escaped. Also HTML entities always end with a semicolon or they are invalid, as such all your question marks after the ; are unneeded (or rather harmful).

Then the actual problem with you expression was the following: The inner group with the negative look-ahead for " expected 1 or more matches (because of the +). In case of the T as the single text, that T was already occupied by the . afterwards (which is correctly required to match the last character for which the negative look-ahead does apply). Now there was no character left to actually match the look-ahead expression (which required one match though). So what did the regexp do instead? It took the semicolon that was marked as optional (because of the ?) and pulled it inside of the capturing group. So that explains where the semicolon comes from.

If you remove the question marks after the semicolons as advised above, then you run in the problem that the regexp does not match the T at all (because it is expecting two or more characters). So the solution is to allow no character to match the negative look-ahead expression (i.e. a * instead of the +). And then, if you want to make the regexp even better, allow empty sequences inside the quotes by adding a ? to the final . too. And then you should have a working expression.

But of course, given that this is encoded using HTML entities, it might be a better idea to simply decode those first, and match directly on the quotes then, as Matteo suggested. This answer is merely to explain what was wrong with your expression :)

Sign up to request clarification or add additional context in comments.

Comments

1

Why don't you decode it first and process that version of the string? If I understand your requirements you could try this:

$str = '<p class="text1" target="701_text" flag:text1="T">';

$decoded = html_entity_decode($str);

preg_match('/flag:(.+?)="(.+?)"/', $decoded, $match);

print_r($match);

1 Comment

Looks like this may do, I was already adding (.*)= reloaded and saw you had changed it also. ;) Thanks for the reply.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.