简体   繁体   中英

RegEx does not match my example

This RegEx could not find example string.

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. What am I missing?

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.

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.

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.
  3. ALTER\\s+TABLE\\s+ADMIN_\\s+ADD\\s+CONSTRAINT This will match ALTER TABLE ADMIN_ ADD CONSTRAINT , regardless of the spacing used.
  4. \\s+(.+)\\s+ Then, the next space-bound word(s)** will be captured into the first group. You're accepting any character here! Maybe you could want to restrict that to \\w+ or the like. 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.
  6. \\s*\\((.+)\\) This will capture anything inside the parentheses as the PK in the second capture group.
    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)
    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.
  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)

** Note that in 4, the inner expression .+ will take as many characters as possible, as long as the remaining parts also match the string. 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); , capturing 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.

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)

In JavaScript you only need to escape backslashes that are part of escape sequences when you're composing a regexp from a string, eg:

 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):

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

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