Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ugly Regexp

Status
Not open for further replies.

SCelia

Programmer
Feb 27, 2002
82
CA
I'm trying to match all <input> where type=text and then extract the name and value params. Because type, name, and value could be in any order I used the ? quantifier on the value and text params. Unfortuantly this also prevents the matched values from being stored in the $1,$2,$3,... variables. Is there a good way to get around this?

/<input[^>]*(value=&quot;?'?[^'&quot;]+&quot;?'?)?[^>]*(type=&quot;?'?text&quot;?'?)?[^>]*(value=&quot;?'?[^'&quot;]+&quot;?'?)?[^>]*name=&quot;?'?([^>\s&quot;]+)&quot;?'?[^>]*(value=&quot;?'?[^'&quot;]+&quot;?'?)?[^>]*(type=&quot;?'?text&quot;?'?)?[^>]*(value=&quot;?'?[^'&quot;>]+&quot;?'?)?[^>]*>/ Celia
 
Here's something you can tinker with. Hopefully it will give you some ideas.
Code:
while (<>) {
    my %inputs;
    if (/(<input[^>]+)/) {
        %inputs = $1 =~ /(\w+)=&quot;?(\w+)&quot;?/g;
    }
    next unless $inputs{type} eq 'text';
    # Do something with key/values in %input ...
    # They will be reset the next time through the loop
    print &quot;-----\n&quot;;
    print &quot;$_ => $inputs{$_}\n&quot; for keys %inputs;
    # Or
    print &quot;name => $inputs{name}\n&quot; if exists $inputs{name};
    print &quot;type => $inputs{type}\n&quot; if exists $inputs{type};
}

jaa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top