Regular Expression Problem

Discussion in 'Perl Misc' started by Kenneth Baltrinic, Jul 2, 2003.

  1. I am trying to parse a relatively simple SQL query with a regular
    expression. All is going well except for one issue I don't seem to be able
    to find a solution for. Handling optional parentheses and brackets. This
    seems like a back reference problem to me but I am not sure. Let me give a
    simple example.

    An table name might be enclosed in brackets or it might not. For the sake of
    simplification, lets assume that the pattern [A-Za-z_]+ is what we are
    looking for in a table name. A really simple solution to find table names
    that are bracket enclosed or not would as follows:

    /(\[[A-Za-z_]+\])|([A-Za-z_]+)/

    The problem is that the pattern for a table name is not in reality as simple
    as [A-Za-z_]+ . In fact it is quite long, and repeating it twice in the
    expression seems inefficient not to mention prone to bugs if I need to tweak
    it and don't get each side exactly the same. What I want to do is something
    like the following:

    /(\[?)[A-Za-z_]+\1/

    This almost works except of course that my backreference is looking for an
    opening bracket [ when I need it to be looking for a closing bracket ]. So
    here it the crux of my question. Is there any way to do something like
    this--to have a backreference that does some sort of fuzzy match? I have a
    similar issues with parentheses.

    Thanks for the help,
    Ken Baltrinic
    Kenneth Baltrinic, Jul 2, 2003
    #1
    1. Advertising

  2. Kenneth Baltrinic

    Sam Holden Guest

    On Wed, 02 Jul 2003 04:33:24 GMT,
    Kenneth Baltrinic <> wrote:
    > I am trying to parse a relatively simple SQL query with a regular
    > expression. All is going well except for one issue I don't seem to be able
    > to find a solution for. Handling optional parentheses and brackets. This
    > seems like a back reference problem to me but I am not sure. Let me give a
    > simple example.
    >
    > An table name might be enclosed in brackets or it might not. For the sake of
    > simplification, lets assume that the pattern [A-Za-z_]+ is what we are
    > looking for in a table name. A really simple solution to find table names
    > that are bracket enclosed or not would as follows:
    >
    > /(\[[A-Za-z_]+\])|([A-Za-z_]+)/
    >
    > The problem is that the pattern for a table name is not in reality as simple
    > as [A-Za-z_]+ . In fact it is quite long, and repeating it twice in the
    > expression seems inefficient not to mention prone to bugs if I need to tweak
    > it and don't get each side exactly the same. What I want to do is something
    > like the following:


    Why not something like:

    (\[$table_name_pattern])|($table_name_pattern)

    Of course if nested parentheses are needed things get more complicated, and
    moving to a non-regex solution is often easier. Something like
    Parse::RecDescent, for example.

    >
    > /(\[?)[A-Za-z_]+\1/
    >
    > This almost works except of course that my backreference is looking for an
    > opening bracket [ when I need it to be looking for a closing bracket ]. So
    > here it the crux of my question. Is there any way to do something like
    > this--to have a backreference that does some sort of fuzzy match? I have a
    > similar issues with parentheses.


    Can't help with that, sorry...

    --
    Sam Holden
    Sam Holden, Jul 2, 2003
    #2
    1. Advertising

  3. > What I want to do is something like the following:
    >
    > /(\[?)[A-Za-z_]+\1/
    >
    > This almost works except of course that my backreference is looking
    > for an opening bracket [ when I need it to be looking for a closing
    > bracket ]. So here it the crux of my question. Is there any way to
    > do something like this--to have a backreference that does some sort
    > of fuzzy match? I have a similar issues with parentheses.
    >
    > Thanks for the help,
    > Ken Baltrinic


    Maybe a (?<= [ )] positive lookbehind is what you are looking for...

    I'm pretty new to this though, and if what you are showing is just a small
    part of the regex, it may not work.

    XC
    Chauncey Williams, Jul 2, 2003
    #3
    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. godfather2

    Regular Expression problem

    godfather2, Jul 20, 2003, in forum: Perl
    Replies:
    1
    Views:
    992
    Shawn Corey
    Jul 20, 2003
  2. VSK
    Replies:
    2
    Views:
    2,268
  3. Nazir
    Replies:
    3
    Views:
    5,047
    jayendra gadhavi
    Jan 2, 2008
  4. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    829
    Alan Moore
    Dec 2, 2005
  5. GIMME
    Replies:
    3
    Views:
    11,918
    vforvikash
    Dec 29, 2008
Loading...

Share This Page