Locked Regular expression problem

  • Saturday, January 14, 2012 4:11 PM
     
     

    Hi,


    I'm searching a regex to filter out the following data

    for example the text:

    select *
    from schema.[table name] d

    i want to filter out the from component in 3 groups

    1st group = from
    2d group = schema.[table name]
    3d group = d

    the regex that i use (FROM)\s+([\[\]\w\.]+)\s+(AS\s+)?(\b\w+\b) does the trick but can't handle the spaces between the brackets so it only works when there are no spaces

    select *
    from schema.[tablename] d

    How do change this regex expression so the ([\[\]\w\.]+) can also match the schema.[table name]

     

    Thanks

All Replies

  • Sunday, January 15, 2012 11:47 PM
     
     Answered Has Code

    Assuming that the schema is optional, and so are the brackets, you can use something like this:

    @"(FROM)\s+((?:\w+\.)?(?:(?:\[[\w\s]+])|(?:\w+)))\s+(?:AS\s+)?(\w+)\b"
    

    The pattern above uses non-capturing groups extensively so that the result groups aren't littered with extra captures. Adjust as you see fit.

    Off topic: most special characters lose their special meaning in a character class (where it wouldn't make sense). This also apply to the dot, so you can just use "[ab.]" instead of "[ab\.]".

    HTH
    --mc