简体   繁体   English

正则表达式与我的示例不匹配

[英]RegEx does not match my example

This RegEx could not find example string. 此RegEx找不到示例字符串。

RegEx: 正则表达式:

^ALTER\\sTABLE\\sADMIN_\\sADD CONSTRAINT \\s(.*)\\sPRIMARY KEY \\s(\(.*\))\\.([a-zA-Z0-9_]+)

Example: 例:

ALTER TABLE ADMIN_ ADD CONSTRAINT PK_ADMIN_ PRIMARY KEY (RECNOADM);

I am new to regex and tried to complete my RegEx at REGEX101.COM but with no success. 我是regex的新手,并试图通过REGEX101.COM完成我的RegEx,但没有成功。 What am I missing? 我想念什么?

Djorjde Djorjde

^\\s*ALTER\\s+TABLE\\s+ADMIN_\\s+ADD\\s+CONSTRAINT\\s+(.+)\\s+PRIMARY\\s+KEY\\s*\\((.+)\\)\\s*;\\s*$

This expression will match the SQL statement you used as an example, capturing PK_ADMIN_ in the first group and RECNOADM in the second. 此表达式将匹配你用作示例的SQL语句,捕捉PK_ADMIN_第一组和在RECNOADM中的第二个。

My suggestion is to use always \\s+ to match the spaces ( \\s* when they are optional, like the leading or trailing spaces), unless they have to be exactly a single space. 我的建议是始终使用\\s+来匹配空格(当它们是可选的时,例如\\s* ,例如前导或尾随空格),除非它们必须完全是一个空格。

So let's break the regex down: 因此,让我们分解一下正则表达式:

  1. ^ Marks the beginning of the line. ^标记行的开头。 You don't want the line to match if there's anything else before. 如果之前还有其他内容,您不希望该行匹配。
  2. \\s* Optional leading spaces. \\s*可选的前导空格。
  3. ALTER\\s+TABLE\\s+ADMIN_\\s+ADD\\s+CONSTRAINT This will match ALTER TABLE ADMIN_ ADD CONSTRAINT , regardless of the spacing used. ALTER\\s+TABLE\\s+ADMIN_\\s+ADD\\s+CONSTRAINT这将匹配ALTER TABLE ADMIN_ ADD CONSTRAINT ,无论使用什么间距。
  4. \\s+(.+)\\s+ Then, the next space-bound word(s)** will be captured into the first group. \\s+(.+)\\s+然后,下一个空格约束的单词**将被捕获到第一组中。 You're accepting any character here! 您在这里接受任何字符! Maybe you could want to restrict that to \\w+ or the like. 也许您可能希望将其限制为\\w+等。 Unless you accept an empty group, use the + closure (ie, one or more), not the * one (ie, zero or more) 除非您接受一个空组,否则请使用+闭包(即一个或多个),而不要使用*一个(即零或多个)
  5. PRIMARY\\s+KEY Matches the sequence PRIMARY KEY , again, regardless of the spacing. PRIMARY\\s+KEY再次匹配序列PRIMARY KEY ,而不管其间距如何。
  6. \\s*\\((.+)\\) This will capture anything inside the parentheses as the PK in the second capture group. \\s*\\((.+)\\)这将捕获括号内的任何内容作为第二个捕获组中的PK。
    1. \\s* Means that it can be optionally preceded by an arbitrary number of spaces (although they are optional. They are in SQL if I recall correctly) \\s*表示可以选择在其前面加上任意数量的空格(尽管它们是可选的。如果我没记错的话,它们在SQL中)
    2. \\( ... \\) You have to escape the parentheses because they are characters to match, no special characters of the regex. \\( ... \\)您必须对括号进行转义,因为它们是要匹配的字符,而不是正则表达式的特殊字符。
    3. (.+) Here you capture (between unescaped parentheses) everything between the (escaped) parentheses into a capture group. (.+)在这里,您(在未转义的括号之间)将(转义的)括号之间的所有内容捕获到捕获组中。 The second one in this case. 在这种情况下,第二个。
  7. \\s*;\\s* The sentence has to end with a semicolon, optionally preceded and/or succeeded by any spaces. \\s*;\\s*句子必须以分号结尾,可以选择在任何空格之前和/或之后加上空格。
  8. $ Marks the end of the line. $标记行的结尾。

In case you want to accept more than one sentence in the same line, you'd remove the ^ and $ zero-width delimiters. 如果您想在同一行中接受多个句子,则可以删除^$零宽度定界符。

About the escaping, the easiest way here is to simply double every backslash in the expression you built in the editor: ^\\\\s*ALTER\\\\s+TABLE\\\\s+ADMIN_\\\\s+ADD\\\\s+CONSTRAINT\\\\s+(.+)\\\\s+PRIMARY\\\\s+KEY\\\\s*\\\\((.+)\\\\)\\\\s*;\\\\s*$ However, there are context and/or languages where a more complex escaping may be needed (eg, the Linux shell) 关于转义,这里最简单的方法是简单地将编辑器中构建的表达式中的每个反斜杠加倍: ^\\\\s*ALTER\\\\s+TABLE\\\\s+ADMIN_\\\\s+ADD\\\\s+CONSTRAINT\\\\s+(.+)\\\\s+PRIMARY\\\\s+KEY\\\\s*\\\\((.+)\\\\)\\\\s*;\\\\s*$但是,在某些情况下存在上下文和/或语言可能需要更复杂的转义(例如,Linux shell)

** Note that in 4, the inner expression .+ will take as many characters as possible, as long as the remaining parts also match the string. **请注意,在4中,只要其余部分也与字符串匹配,则内部表达式.+将采用尽可能多的字符。 This is because the closures are by default greedy, meaning that the engine will try to match the longest string possible. 这是因为默认情况下,闭包是贪婪的,这意味着引擎将尝试匹配可能的最长字符串。 That means that, for instance, this entry will match: ALTER TABLE ADMIN_ ADD CONSTRAINT PK_ADMIN_ OR *WHATEVER* YOU "WANT" TO PUT HERE! PRIMARY KEY (RECNOADM); 例如,这意味着该条目将匹配: ALTER TABLE ADMIN_ ADD CONSTRAINT PK_ADMIN_ OR *WHATEVER* YOU "WANT" TO PUT HERE! PRIMARY KEY (RECNOADM); ALTER TABLE ADMIN_ ADD CONSTRAINT PK_ADMIN_ OR *WHATEVER* YOU "WANT" TO PUT HERE! PRIMARY KEY (RECNOADM); , capturing PK_ADMIN_ OR *WHATEVER* YOU "WANT" TO PUT HERE! ,捕获PK_ADMIN_ OR *WHATEVER* YOU "WANT" TO PUT HERE! in the first group. 在第一组中。 Hence the importance of restricting the set of accepted characters ;) 因此,限制接受字符集的重要性;)

Have you tried the following? 您是否尝试过以下方法?

^ALTER\\sTABLE\\sADMIN_\\sADD\\sCONSTRAINT\\s((.*))\\sPRIMARY\\sKEY\\s\\((.*)\\);

I am wrapping two separate blocks through () in order to identify from the Regex the values inserted if you need to access them too. 我通过()包装了两个单独的块,以便从Regex标识插入的值(如果您也需要访问它们)。

In your regex there are few issues with white spaces (mixing up white spaces with \\s and the white space should be \\s not \\s) 在您的正则表达式中,空格几乎没有问题(将空格与\\ s混合使用,空格应为\\ s而不是\\ s)

In JavaScript you only need to escape backslashes that are part of escape sequences when you're composing a regexp from a string, eg: 在JavaScript中,当您从字符串组成正则表达式时,只需要转义转义序列中的反斜杠即可,例如:

 var r = new RegExp('\\\\d'); console.log(r.test('2')); 

But the additional \\ is not part of the regexp and you don't need it when using the literal syntax (or regexp101): 但是,附加的\\并不是regexp的一部分,使用文字语法(或regexp101)时,您不需要它:

 var r = /\\d/; console.log(r.test('2')); 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM