Ignore quoted text in reg exp

Discussion in 'Perl Misc' started by nick, Nov 29, 2003.

  1. nick

    nick Guest

    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
    nick, Nov 29, 2003
    #1
    1. Advertising

  2. nick

    Bigus Guest

    "nick" <> wrote in message
    news:...
    > 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
    Bigus, Nov 29, 2003
    #2
    1. Advertising

  3. nick

    nick Guest

    "Bigus" <> wrote in message news:<M0_xb.1543$>...
    > "nick" <> wrote in message
    > news:...
    > > 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
    nick, Nov 29, 2003
    #3
  4. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    (nick) wrote in
    news::

    > 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-----
    Eric J. Roode, Nov 29, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew Rowland

    Reg exp: matching relative path only.

    Andrew Rowland, Aug 2, 2003, in forum: Perl
    Replies:
    0
    Views:
    1,135
    Andrew Rowland
    Aug 2, 2003
  2. psk

    Newbie-Reg Exp

    psk, Jan 16, 2004, in forum: Perl
    Replies:
    2
    Views:
    1,352
    Gunnar Hjalmarsson
    Jan 19, 2004
  3. PerlE

    Reg Exp Help

    PerlE, Jan 30, 2004, in forum: Perl
    Replies:
    0
    Views:
    496
    PerlE
    Jan 30, 2004
  4. Ken Chesak

    reg exp

    Ken Chesak, Aug 26, 2004, in forum: Perl
    Replies:
    0
    Views:
    618
    Ken Chesak
    Aug 26, 2004
  5. Ken Chesak

    reg exp

    Ken Chesak, Aug 30, 2004, in forum: Perl
    Replies:
    5
    Views:
    640
Loading...

Share This Page