简体   繁体   中英

Recursive match with regular expression

I need to split an expression with the following strings:

'with', 'select', 'from', 'where'

These will come in the sequence as I wrote. You may assume that each of the words will begin and end with word boundary.

I have done this without any problem. However, a new requirement has arrived. The from clause may contain a whole expression.

That means, the expression may be something like this:

with
   something
select
   something more
from
   with
      aaaa
   select
      bbbb      
   from
      [may be another expression]
   where
      the inner expression ends here
where
   the outer expression ends here

I need not split the inner expression.

Now the question is, is there a standard way to do something like that? Or I need to do it totally on my own manually?

I am not sure if it is important, my application is a C# one.

It looks like your use case is starting to get complicated enough that you might be better off investing the time and refactoring effort to use actual parsing utilities, rather than regular expressions. I'm not a C# programmer, so I can't help you much with suggestions, but googling for "C# parser libraries" yielded several links that looked promising.

If you only need up to a fixed level of nesting then I think it technically can be done in regular expressions, but it's going to get nasty. Probably the simplest approach would be to write an expression for each fixed level of nesting, and match them starting from the top until you get a hit. Ugly, but could work.

If there's no limit to the number of nested expressions that could be contained in a top-level expression, then regular expressions are not powerful enough to do this properly, and you'll need to look at parsing utilities. I would not attempt to write a parser on your own; there's almost certainly usable libraries that solve most of the tricky problems for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM