简体   繁体   中英

Match the pattern TEXT[0,0]

I have a lot of source codes with SQL queries like below:

c_query := "SELECT * FROM TABLE WHERE FIELD_NAME_ONE[2] = 'AB' AND FIELD_NAME_TWO[1,8] = 'ABCDEFGH'"

I would like to match these: FIELD_NAME_ONE[2] and FIELD_NAME_TWO[1,8] and these patterns must be found between double quotes ( " ).

Edit

c_query := "SELECT * FROM TABLE WHERE FIELD_NAME_ONE[2] = 'AB' AND FIELD_NAME_TWO[1,8] = 'ABCDEFGH' AND TESTE[9] = 'XXXXXXXXX' AND FOO = '" + is_an_array[2] + "'"

It shouldn´t match is_an_array[2] because is not inside the double quotes.

Here's a regex pattern for what you seek (edited based on comment):

".*?[A-Z_]+\[\d(,\d)?\].*?"

I made the match non-greedy ( .*? ) in case there are multiple matches on the one line (unlikely, but for completeness...)

Edited

It's not clear if you want to match just the target field names or the whole SQL. If you want to match just the field names alone, use non-capturing groups for the rest:

(?:".*?)[A-Z_]+\[\d(,\d)?\](?:.*?")

I'm assuming you want to be able to match more than just those two specific fields, otherwise you wouldn't have gone to the trouble of applying regular expressions:

var tokens = Regex.Matches(sql, "\"([^\"]+)\"");

foreach (Match token in tokens) {
    string str = token.Groups[1].Value;

    var fields = Regex.Matches(str, @"(\w+\[\d+(,\d+)*\])");

    foreach (Match field in fields)
        Console.WriteLine(field.Value);
}

This will find any sequence of letters, numbers and underscores followed by square brackets, with 1 or more comma-separated numbers.

If you only want to match a sequence of letters and underscores before the square brackets, ammend the pattern to:

@"([a-zA-Z_]+\[\d+(,\d+)*\])"

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