Ignore quoted text in reg exp

N

nick

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
 
B

Bigus

nick said:
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
 
N

nick

Bigus said:
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
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

(e-mail address removed) (nick) wrote in
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-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top