Regex backreference within group with OR

-1

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

CREATE TABLE ABC;
ALTER TABLE ABC;

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

(CREATE\s+TABLE\s+(\w+).+;|ALTER\s+TABLE\s+.+;)
    
asked by anonymous 26.12.2018 / 15:21

1 answer

1

You do not need the "OR" ( | ) because it means that only one of the snippets will already satisfy the expression. In this case, you have "CREATE" on one side of | and "ALTER" on the other. When regex captures one of them, you'll already consider the expression satisfied, and will not evaluate the rest . p>

In your case, regex finds "CREATE" and so on. Since ALTER is on the other side of | , it no longer needs to be evaluated (since a match was already found in "CREATE").

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

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

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

Check here for this regex running.

It's not clear what language you're using, but usually \s already considers line breaks, so putting \s* before "ALTER" causes both space and line break to be considered. p>

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

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

Or, if you want to be more detailed, use (?:\r?\n)+ -% carriage return ) used in Windows along with% in>), whereas on Unix systems only \r is used. Thus, \n makes \n optional and this expression considers line breaks in both systems. Then, \r?\n causes the expression to be a non-catch group , not to create one more group at random (depending on the position of the group, this can change the value of backreference \r ). And (?: causes one or more line breaks to be considered.

    
26.12.2018 / 16:39