Sushant Gupta
Sushant Gupta

Reputation: 1527

Regex non-capturing parenthesis issue

I have a database query which looks like this

select * from students join (select * from teachers) join (select * from workers

I had a requirement to tokenize this string based on 'select'. I am trying regex (select)(.*?)((?:select)|$), ut it is matching only 2 times. Request some pointers on how to achieve this.

I need the 3 output tokens as below

Upvotes: 0

Views: 98

Answers (2)

The Guy with The Hat
The Guy with The Hat

Reputation: 11132

I think this regex will work:

select.*?(?=select|$)

The regex matches the word select, then any text (not including new lines) up until right before the next select or the end of the string.

Demonstration here: http://regex101.com/r/sR3gV1

Upvotes: 2

Sabuj Hassan
Sabuj Hassan

Reputation: 39355

If you are trying to parse the select queries from the string then you can use this regex. Assuming you are not doing select from multiple tables(i.e. not doing select * from x,y,z)

(select.*?from\\s+\\w+)

Upvotes: 0

Related Questions