Regex backreference within group with OR

Asked

Viewed 60 times

-1

I am trying to reference a value within an "OR". Example, I have the text inside the file:

CREATE TABLE ABC;
ALTER TABLE ABC;

I need the ABC (table name) of ALTER always be equal to CREATE, I have tried the following expression but without success.

(CREATE\s+TABLE\s+(\w+).+;|ALTER\s+TABLE\s+\2.+;)

1 answer

1

You don’t need the "OR" (|), because it means that only one of the excerpts will already satisfy the expression. In this case, you have the "CREATE" on one side of the | and the other’s "ALTER". When the regex captures one of them, it will already consider that the expression has been satisfied and will not evaluate the rest.

In your case, regex finds the "CREATE" and stops there. As the "ALTER" is on the other side of |, it no longer needs to be evaluated (as it has already been found a match in the "CREATE").

If you want to check the entire text (both "CREATE" and "ALTER"), remove the |.

Another detail is that between the table name and the ; no spaces, but you used .+ (one or more characters). In this case, how can you have spaces between the table name and the ;, but also may not have any, I suggest changing to \s* (zero or more spaces).

(CREATE\s+TABLE\s+(\w+)\s*;\s*ALTER\s+TABLE\s+\2\s*;)

See here this regex in operation.


It is not clear what language you are using, but generally the \s already consider the line breaks, so put the \s* before "ALTER" makes both spaces and line breaks be considered.

But if you want to force yourself to have at least one line break, you can add a [\n\r]+:

(CREATE\s+TABLE\s+(\w+)\s*;[\n\r]+\s*ALTER\s+TABLE\s+\2\s*;)

Or, if you want to be more detailed, use (?:\r?\n)+ - the \r (Carriage Return) is used in Windows along with \n (line feed), while on Unix systems only the \n is used. So, \r?\n causes the \r is optional and this expression considers line breaks on both systems. Next, (?: makes the expression a catch group, not to create another group for no reason (depending on the position of the group, this can change the value of backreference \2). And the + causes one or more line breaks to be considered.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.