3
votes

I'm not too familiar with VB script and am having trouble with the syntax for the pattern property of my regexp object.

I have some data that looks like this:

Front SHD Trip CLEAR OBSTRUCTION BEFORE CONTINUING [Table Position = 0mmFwd] Front SHD Trip CLEAR OBSTRUCTION BEFORE CONTINUING [Table Position = 563mmFwd]

I want to strip off the [Table Position = x] part from these records so I have created a little function. Although there are no errors, it's not stripping off the end of the string as expected and I'm fairly sure that the issue is my syntax in this line:

objRegExp.Pattern = "[*]"

Here's the whole function:

Function RemoveTablePosition(AlarmText)

'Initialise a new RegExp object
Dim objRegExp, strNewAlarmText
Set objRegExp = New Regexp

'Set the RegExp object's parameters
objRegExp.IgnoreCase = True
objRegExp.Global = True

'Look for [Table Position = xx] at the end of the code text (they always follow the same format)
objRegExp.Pattern = "[*]"

'Replace all [Table Position = xx] with the empty string to effectively remove them
strNewAlarmText = objRegExp.Replace(AlarmText, "")

'Return the new alarm text value
RemoveTablePosition = strNewAlarmText
Set objRegExp = Nothing

End Function

Can someone point me in the right direction? Thanks in advance!

2
You need \[[^\][]*] or \[[^\][]*]$ ($ - end of string/line) - Wiktor Stribiżew

2 Answers

2
votes

"[*]" is a character class matching a * literal character.

You can use

\[[^\]]*]$

or

\[.*?]$

See the regex demo. If you need to also match optional whitespace(s) before the [...], add \s* at the pattern start.

Explanation

  • \[ - literal [ symbol
  • [^\]]* - zero or more symbols other than ] (if there can be no [ and ], replace this one with [^\][]*)
    OR
  • .*? - 0+ any characters other than a newline as few as possible up to the first...
  • ] - a literal ] symbol that is at the...
  • $ - end of string

The difference between \[[^\]]*]$ and \[.*?]$ is that the former will also match newlines in between [ and ] (if any) and \[.*?]$ won't.

0
votes

Assuming you also wish to get rid of the space after "CONTINUING", you would want to search for: " \[.*\]$"

This looks for the space, left bracket, any number of characters, a right bracket and end of string.

If the right bracket is not end of string, for example:

Test String [REMOVE [THIS] AND CONTINUE]

it will keep matching with .* until it finds \]$ ($ being end of string.)

Wiktor's examples are good, but "\[[^\]]*]$" is broken - I think he meant "\[[^\]]*\]$" - the problem with this is that it will stop at the first right bracket, so the example I gave above would fail to match

His second example, "\[.*?]$", also should probably be "\[.+\]$" assuming you wish to ensure there is something within the brackets, which it appears he was going for.
(? looks for 0 or 1, + looks for 1 or more, * looks for 0 or more)

(More elegant to use "\[.*\]$" as this does not make that assumption. ".*" matches 0 or more of any character. Surrounded by \[ and \], it will find anything in a pair of brackets, and ending with the $ assures it is at end of string.)

Hopefully this helps someone - I noted the date before replying, but saw the broken regex. The way the site uses * and hides single \ marks, it could be Wiktor's examples were fine until he posted them... (More than likely, actually.)