Ben
Ben

Reputation: 1295

Regex to find period in square brackets

I am parsing some SQL statements and have found places where the SELECT statement may be:

SELECT [tblCustomer].[FirstName], [tblCustomer.LastName], [tblOrder].[Order_No]

and as you see the second column has a . inside the square brackets. This is acceptable in Access SQL but not SQL Server. I'm trying to build a RegEx to identify when there is a . inside square brackets and replace it with ].[

I've tried: \[.+?\](?![\.]) which will get me a period inside square brackets but it doesn't stop searching when it finds the closing bracket.

I'm using ECMAScript to be compatible with VBA and I don't have concerns about nested brackets.

Example: https://regex101.com/r/Inxhdg/1/

Upvotes: 3

Views: 630

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626748

You can use

Search for: (\[\w+)\.(?=\w+])
Replace with: $1].[

See the regex demo. Details:

  • (\[\w+) - Group 1 ($1): [ and then any one or more letters, digits, or underscores
  • \. - a dot
  • (?=\w+]) - a positive lookahead that requires one or more letters, digits or underscores and then a ] char immediately to the right of the current location.

Upvotes: 1

Related Questions