Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Ignore quoted text in reg exp

Reply
Thread Tools

Ignore quoted text in reg exp

 
 
nick
Guest
Posts: n/a
 
      11-29-2003
I can't seem to figure out how to ignore quoted text in a regular
expression. For example, I would like a reg exp to match 1-3, but not
4:

my name is nick
my "string"name"string" is nick
my "text" name "text" is nick
my "name" is nick

Is this even possible?

Thanks for any help.

Nick
 
Reply With Quote
 
 
 
 
Bigus
Guest
Posts: n/a
 
      11-29-2003

"nick" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> I can't seem to figure out how to ignore quoted text in a regular
> expression. For example, I would like a reg exp to match 1-3, but not
> 4:
>
> my name is nick
> my "string"name"string" is nick
> my "text" name "text" is nick
> my "name" is nick


Well, you could do something like this:

@str = (
'my name is nick',
'my "string"name"string" is nick',
'my "text" name "text" is nick',
'my "name" is nick'
);

foreach (@str) {
($temp = $_) =~ s/".*?"//g;
if ($temp =~ /my.*name.*is.*nick/i) { print "found in $_\n"; }
};

What that is doing is using a temp variable to hold the contents of $_ (so
as to avoid wrecking your original array), remove anything in quotes and
then search for 'my name is nick'.

Bigus


 
Reply With Quote
 
 
 
 
nick
Guest
Posts: n/a
 
      11-29-2003
"Bigus" <(E-Mail Removed)> wrote in message news:<M0_xb.1543$(E-Mail Removed)>...
> "nick" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
> > I can't seem to figure out how to ignore quoted text in a regular
> > expression. For example, I would like a reg exp to match 1-3, but not
> > 4:
> >
> > my name is nick
> > my "string"name"string" is nick
> > my "text" name "text" is nick
> > my "name" is nick

>
> Well, you could do something like this:
>
> @str = (
> 'my name is nick',
> 'my "string"name"string" is nick',
> 'my "text" name "text" is nick',
> 'my "name" is nick'
> );
>
> foreach (@str) {
> ($temp = $_) =~ s/".*?"//g;
> if ($temp =~ /my.*name.*is.*nick/i) { print "found in $_\n"; }
> };
>
> What that is doing is using a temp variable to hold the contents of $_ (so
> as to avoid wrecking your original array), remove anything in quotes and
> then search for 'my name is nick'.
>
> Bigus


Thanks. Unfortunately it doesn't really work for my purposes. (Well,
it might, but it would end up getting messy.) I need to search in the
original string since I'm doing some replacing. I am hoping there is a
clean way to do this.

What I'm doing is searching for the tag <script runat="server"> and
replacing it with <TAG><script runat="server"></TAG>. However, the
runat="server" cannot be quoted, as in <script 'runat="server"'>.
There can also be other parameters in the runat tag, such as <script
language="jscript" runat="server">. In this case the replacement would
be <TAG><script language="jscript" runat="server"></TAG>. So, I can't
simply strip out the quoted text.

Any ideas?

Thanks.

Nick
 
Reply With Quote
 
Eric J. Roode
Guest
Posts: n/a
 
      11-29-2003
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

http://www.velocityreviews.com/forums/(E-Mail Removed) (nick) wrote in
news:(E-Mail Removed) om:

> What I'm doing is searching for the tag <script runat="server"> and
> replacing it with <TAG><script runat="server"></TAG>. However, the
> runat="server" cannot be quoted, as in <script 'runat="server"'>.
> There can also be other parameters in the runat tag, such as <script
> language="jscript" runat="server">. In this case the replacement would
> be <TAG><script language="jscript" runat="server"></TAG>. So, I can't
> simply strip out the quoted text.
>
> Any ideas?


If you're parsing HTML(XML), use a module for parsing HTML(XML). Regexes
are going to be a lot of work for that task, and will likely not handle
all the cases you need it to.

Also, it seems very strange that you're putting a tag pair around just a
start tag.

- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBP8j09WPeouIeTNHoEQIacgCeMXPy/H4wmCraJDneKDhICLGo54kAn3r7
+Z/VZU92BaFlKO0Pa+dK3dRo
=N91t
-----END PGP SIGNATURE-----
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reg exp Ken Chesak Perl 5 09-01-2004 12:27 PM
reg exp Ken Chesak Perl 0 08-26-2004 03:40 PM
Reg Exp Help PerlE Perl 0 01-30-2004 06:15 AM
Newbie-Reg Exp psk Perl 2 01-19-2004 10:30 PM
Reg exp: matching relative path only. Andrew Rowland Perl 0 08-01-2003 11:14 PM



Advertisments