Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Pattern Matching Problem

by Anonymous Monk
on Aug 06, 2003 at 12:42 UTC ( #281370=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a new perl problem, that I haven't quite figured out the Regular Expression for. We have a web based form that submits data in paragraph form with variables from the form entered into it. A typical set of data may look like this.
Patron @FirstName@ @LastName@ has filled out the @FormName@ form. Plea +se send them an email at @EmailAddress@.
Where FirstName, LastName, FormName, and EmailAddress are the actual values, not the variable names. What I need to do, is highlight each of those variables by placing a <B> after the first "@" and a </B> before the second "@", in each one of those variables (without removing the "@"). Best I can come up with is going through the string one character at a time, and watching which "@" I am on. There has got to be a better way to perform this. Iterating over the whole string like that seems sloppy.

Replies are listed 'Best First'.
Re: Pattern Matching Problem
by Abigail-II (Bishop) on Aug 06, 2003 at 13:01 UTC
    s!\@([^\@]*)\@!\@<B>$1</B>\@!g;

    Abigail

      Ok, so this one catches a string of any length (including zero), using *. The string can be any character except "@" [^\@]. The matching expression on the left captures the non-@ string and puts it into the variable $1, which is then put between bold tags. Abigail-II has used exclamation points here to improve legibility.

      --
      Allolex

Re: Pattern Matching Problem
by allolex (Curate) on Aug 06, 2003 at 13:14 UTC
    #!/usr/bin/perl $_ = 'Patron @FirstName@ @LastName@ has filled out the @FormName@ form +. Please send them an email at @EmailAddress@.'; s/@([a-zA-Z]+?)@/<b>\@$1\@<\/b>/g; print $_ . "\n"; # Output: Patron <b>@FirstName@</b> <b>@LastName@</b> has filled out t +he <b>@FormName@</b> form. Please send them an email at <b>@EmailAddr +ess@</b>.

    This gets the right output with the sample data you gave. I hope it helps you along.

    --
    Allolex

    Update 2003-08-06 15:28:15 CEST: Fixed substitution problem pointed out by Abigail-II (Thanks!).

      That removes the @ signs, of which the OP explicitely mentions they shouldn't be removed.

      Abigail

Re: Pattern Matching Problem
by ph0enix (Friar) on Aug 06, 2003 at 13:19 UTC
    my $text = 'Patron @FirstName@ @LastName@ has filled out the @FormName +@ form. Please send them an email at EmailAddress@.'; my $c=0; $text =~ s/\@([^\@]+)/$c++;($c%2)?"\@<b>$1<\/b>":"\@$1"/eg;
Re: Pattern Matching Problem
by monktim (Friar) on Aug 06, 2003 at 20:23 UTC
    Here you go.
    use strict; use warnings; my $str = 'Patron @FirstName@ @LastName@ has filled out the @FormName@ + form. Please send them an email at @EmailAddress@.'; print "$str\n"; $str =~ s/\@(.*?)\@/\@<B>$1<\/B>\@/g; print "$str\n";
Re: Pattern Matching Problem
by eric256 (Parson) on Aug 06, 2003 at 17:04 UTC

    Seemed like a good spot for look behind and lookahead :-)

    my $test = 'Patron @FirstName@ @LastName@ has filled out the @FormName +@ form. Please send them an email at @EmailAddress@.'; $test =~ s/(?<=@) # starts with @ (but don't grab @) ([a-zA-Z]*) # string of letters (?=@) # ends with @ (but don't grab @) /<b>$1<\/b>/xg; # put $1 back with bold tags around it. @ are + preserved. print $test;
    ___________
    Eric Hodges

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://281370]
Approved by ph0enix
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2023-09-25 05:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?