CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Stripping expression from within HTML

 



richardells
Deleted

Aug 23, 2000, 6:57 AM

Post #1 of 4 (2515 views)
Stripping expression from within HTML Can't Post

Hi,

I have been pulling hair out with the \b ^ $ () and STUFF, but can't seem to do what should be a SIMPLE thing!
$stuff="thisisthestuffwithnospaces\nmaybemore\n";
I just want $middle to equal everything in BETWEEN "thisisthe" and "withnospaces" ie $middle="stuff";
I got it working when I have words using the \b metacharacter, but just can't do it without!
It'd probably be quicker to train a monkey!

If anyone can help me out I'll send 'em a ferrari and matching blonde.... probably...


Regards, Richard.


japhy
Enthusiast / Moderator

Aug 24, 2000, 3:17 AM

Post #2 of 4 (2515 views)
Re: Stripping expression from within HTML [In reply to] Can't Post

There are several approaches:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


($wanted) = $string =~ /thisisthe(.*)withnospaces/; # greedy
($wanted) = $string =~ /thisisthe(.*?)withnospaces/; # non-greedy
($wanted) = $string =~ /thisisthe([^w]*(?:\w+[^wi][^w]*)*)/; # non-greedy
($wanted) = $string =~ /thisisthe((? Frown?!withnospaces).)*)/; # non-greedy
</pre><HR></BLOCKQUOTE>


------------------
Jeff "japhy" Pinyan -- accomplished author, consultant, hacker, and teacher



richardells
Deleted

Aug 24, 2000, 4:35 AM

Post #3 of 4 (2515 views)
Re: Stripping expression from within HTML [In reply to] Can't Post

BRILLIANT!! THANKS!!
Duh! - How simple did your version look compared to what I was doing!!! I feel suitably dumb!
I also need a routine to strip the brackets out of a line of text and push letters together if they are single letters seperated by a full stop or a space ie:

A L S (GROUP) LTD
should become
ALS GROUP LTD
and
T.N.T Europe Ltd
should become
TNT EUROPE Ltd

I am transfering a database, and thought it might be faster in PERL than the only way I really know how... BASIC!
If you know how to do the above without spending too much of your valuable time on it please let me know, because I have learned more from you in one night than I have from the book in a whole weekend!

Keep up the great work!

Best Regards,
Richard.


japhy
Enthusiast / Moderator

Aug 24, 2000, 9:20 AM

Post #4 of 4 (2515 views)
Re: Stripping expression from within HTML [In reply to] Can't Post

You'd probably want to use the tr/// operator to remove parentheses:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$str =~ tr/()//d;
</pre><HR></BLOCKQUOTE>

As for single characters separated by . or a space, I'd use the following:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$str =~ s{
\b # word boundary
( # save to $1
[a-zA-Z] ([\ ]) [a-zA-Z] # a letter followed by '.' or ' ' followed by a letter
(?: \2 [a-zA-Z] )* # and 0 or more of that character and a letter
)
\b # and a word boundary
}{
my $abbrev = $1; # store the whole a.b.c to $abbrev
$abbrev =~ s/\Q$2\E//g; # remove the '.' or ' '
$abbrev; # return the new string
}gex;
</pre><HR></BLOCKQUOTE>

------------------
Jeff "japhy" Pinyan -- accomplished author, consultant, hacker, and teacher


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives