1

I have this text file that I need to check:

} else if ("saveAssured".equals(ACTION))         {
   Integer assuredNo = giisAssuredService.saveAssured(assured);

The pattern will include a variable:

var = "saveAssured"
reMethod.Pattern = """& var &""[.]equals\(ACTION\).{\n.\w+?.=.\w+?[.](\w+?)\(\w+?\)"

I need to capture the second 'saveAssured' from the text file. And the '\n' (new line) seems to be not working. Did I used it right? What other steps can I try?

2 Answers 2

5

http://www.regular-expressions.info/dot.html

JavaScript and VBScript do not have an option to make the dot match line break characters. In those languages, you can use a character class such as [\s\S] to match any character. This character matches a character that is either a whitespace character (including line break characters), or a character that is not a whitespace character. Since all characters are either whitespace or non-whitespace, this character class matches any character.

And have a look at https://regex101.com/r/kH3aZ4/1
Test is for JavaScript but since they have the same Regex flavor, that pattern will also work with VBScript.

Dim reMethod
Set reMethod = New RegExp
    reMethod.IgnoreCase = True
    reMethod.Pattern = """saveAssured""\.equals\(ACTION\)[\s\S]*?\{[\s\S]*?\.([^(]*)\("
Sign up to request clarification or add additional context in comments.

3 Comments

It's working now. Thanks. Just got some logic errors in some part of my VBS: stackoverflow.com/questions/29618032/….
Does VBScript not have the MultiLine property? - Ref Regular Expression Programming (Scripting) - Section: Flags
@Lankymart Yes, there's a MultiLine property in VBScript's RegExp object but this question was about dot matches newline behavior and the .Multiline property does not supply that. [\s\S] instead . is the way to go with VBScript.
2

The information around multiline in VBScript expressions is a bit inconsistent. The VBScript RegExp object does support them but the property is not well documented.

From Regular Expression Programming (Scripting)


Flags

In the JScript regular expression /abc/gim, the g specifies the global flag, the i specifies the ignore case flag, and the m specifies the multiline flag.

In VBScript, you can specify these flags by setting the equivalent properties to True.

The following table shows the allowed flags.

JScript flag    VBScript property    If flag is present or property is True

g Global Find all occurrences of the pattern in the searched string instead of just the first occurrence. i IgnoreCase The search is case-insensitive. m Multiline ^ matches positions following a \n or \r, and $ matches positions before \n or \r. Whether or not the flag is present or the property is True, ^ matches the position at the start of the searched string, and $ matches the position at the end of the searched string.

Below is an example of using MultiLine.

Option Explicit
Dim rx: Set rx = New RegExp
Dim matches, match
Dim data: data = Array("This is two lines of text", "This is the second line", "Another line")
Dim txt: txt = Join(data, vbCrLf)
With rx
  .Global= True
  .MultiLine = True
  .Pattern= "line$"
End With

Set matches = rx.Execute(txt)
WScript.Echo "Results:"
For Each match In matches
  WScript.Echo match.Value
Next

Output:

Results:
line
line

The difference with MultiLine set to False.

Output:

Results: line

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.