简体   繁体   English

匹配模式TEXT [0,0]

[英]Match the pattern TEXT[0,0]

I have a lot of source codes with SQL queries like below: 我有很多带有SQL查询的源代码,如下所示:

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 ( " ). 我想匹配这些: FIELD_NAME_ONE[2]FIELD_NAME_TWO[1,8] ,必须在双引号( " )之间找到这些模式。

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] + "'" 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. 它不应该与is_an_array [2]匹配,因为它不在双引号内。

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. 如果你想匹配目标字段的名称或整个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. 这将找到任何字母,数字和下划线的序列,后跟方括号,带有1个或多个以逗号分隔的数字。

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+)*\])"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么正则表达式模式与文本不匹配? - Why regex pattern doesn't match with text? 一种将文本匹配到多个正则表达式模式的快速方法 - A speedy way to match a text to multiple regex pattern 如果匹配发生在子字符串中,如何在文本中找到带有异常的模式匹配? - How to find pattern match in text with exceptions if match occurs within a substring? 匹配一行文本中的正则表达式模式,而不定位引号中的文本 - Match regex pattern in a line of text without targeting the text within quotations 正则表达式:模式匹配任何文本,后跟特定的封闭格式 - Regex: Pattern to match any text followed by specific enclosing format 我的C#正则表达式模式无法匹配标签之间的文本 - My C# Regex pattern fails to match text between tags 尝试使用C#正则表达式来匹配文本文件中的特定模式和模式序列 - Trying to use C# Regular Express to Match a Specific Pattern and Pattern Sequence from a Text File 为什么此正则表达式模式与此文本不匹配? (简单的正则表达式只在中间包含通配符的转义文本) - Why doesn't this regex pattern match this text? (Simple regex just includes escaped text with wildcard in the middle) 如何使用正则表达式与多行模式中间包含特定文本的文本不匹配? - How to not match against text that contains specific text in the middle of a multiline pattern using regular expressions? 字符串末尾的匹配模式 - Match pattern at the end of string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM