Regular expression problem
-
Saturday, January 14, 2012 4:11 PM
Hi,
I'm searching a regex to filter out the following datafor example the text:
select *
from schema.[table name] di want to filter out the from component in 3 groups
1st group = from
2d group = schema.[table name]
3d group = dthe 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] dHow 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
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- Marked As Answer by Paul ZhouModerator Monday, January 23, 2012 8:16 AM

