Home > Back-end >  regex to know if some strings are joined by another string
regex to know if some strings are joined by another string

Time:10-03

I am making a mini-parser of sql to estimate the maximum length of the value that an operation or a function will return. Ex: round (column, 2). For that, I am using regular expressions. For the example I gave, I got the regular expression round\((\w )(,\s*(\d ))?\).

However, I came across these cases

column1||column2||column3||... columnn
concat(column1, column2, ... columnn)

I tried for the first case (although I knew it wouldn't work), with regex like:

(\w \|\|\w )
(\w \|\|\w \|\|)|(\|\|\w \|\|\w )

What regex do you propose to match the above cases? Or rather a more general question: How could I know if n strings are joined with a specific string?

CodePudding user response:

What regex do you propose to match the above cases?

To match column1||column2||column3||... column10 use (column\d||) regex.

>>> import re
>>> m = re.match("(column\d(\|\|)?) ","column1||column2||column3||column4")
>>> m.group(0)
'column1||column2||column3||column4'

Use similar regex for the second case.

  • Related