Friday 25 October 2019

html - What to do Regular expression pattern doesn't match anywhere in string?




I am trying to match type “hidden” fields using this pattern:



//


This is sample form data:







But I am not sure that the type, name, and value attributes will always appear in the same order. If the type attribute comes last, the match will fail because in my pattern it’s at the start.



Question:
How can I change my pattern so it will match regardless of the positions of the attributes in the tag?



P.S.: By the way I am using the Adobe Air based RegEx Desktop Tool for testing regular expressions.


Answer



Contrary to all the answers here, for what you're trying to do regex is a perfectly valid solution. This is because you are NOT trying to match balanced tags-- THAT would be impossible with regex! But you are only matching what's in one tag, and that's perfectly regular.



Here's the problem, though. You can't do it with just one regex... you need to do one match to capture an tag, then do further processing on that. Note that this will only work if none of the attribute values have a > character in them, so it's not perfect, but it should suffice for sane inputs.




Here's some Perl (pseudo)code to show you what I mean:



my $html = readLargeInputFile();

my @input_tags = $html =~ m/
(
(?=[^>]*?type="hidden") # Use lookahead to make sure that type="hidden"
[^>]+ # Grab the rest of the tag...

\/> # ...except for the />, which is grabbed here
)/xgm;

# Now each member of @input_tags is something like

foreach my $input_tag (@input_tags)
{
my $hash_ref = {};
# Now extract each of the fields one at a time.


($hash_ref->{"name"}) = $input_tag =~ /name="([^"]*)"/;
($hash_ref->{"value"}) = $input_tag =~ /value="([^"]*)"/;

# Put $hash_ref in a list or something, or otherwise process it
}


The basic principle here is, don't try to do too much with one regular expression. As you noticed, regular expressions enforce a certain amount of order. So what you need to do instead is to first match the CONTEXT of what you're trying to extract, then do submatching on the data you want.



EDIT: However, I will agree that in general, using an HTML parser is probably easier and better and you really should consider redesigning your code or re-examining your objectives. :-) But I had to post this answer as a counter to the knee-jerk reaction that parsing any subset of HTML is impossible: HTML and XML are both irregular when you consider the entire specification, but the specification of a tag is decently regular, certainly within the power of PCRE.



No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...