1

I'm newbie in VBScript and have a following task:

Get substring from specific string using vbscript.

The source string:

one two alksdjfkl <b> sdklifjklsdjf </b> <b:FileName>Test</b:FileName> jsdhfj rutyier x,mcvn,mcx </b> <b:FileName>Test2222.docx</b:FileName> mvbn,cmvb eiurtyeiurty

I need to get content between and I've tried following:

Set objRegExp = CreateObject("VBScript.RegExp")
Str = "one two alksdjfkl <b> sdklifjklsdjf </b> <b:FileName>Test</b:FileName> jsdhfj rutyier x,mcvn,mcx </b> <b:FileName>Test2222.docx</b:FileName> mvbn,cmvb eiurtyeiurty"
objRegExp.Global = True
objRegExp.Pattern = "^<b:FileName>*</b:FileName>$"
Set objMatches = objRegExp.Execute(Str)
msgbox objMatches.Count
    For i = 0 To objMatches.Count - 1
       Set Match = objMatches.Item(i)
        msgbox Match.Value
    Next

But I didn't get what I really need: Test.doc, Test2222.docx. It seems that I don't understand how regex specific symbols are working. Could you please help me with this task? Thanks in advance!

1
  • The object RegExp is a built-in VBScript object, you do not have to late bind to it using the CreateObject() method. the exception to this is if you are accessing it via VBA which can only access it through COM because it exists in the VBScript runtime. Commented Nov 22, 2019 at 14:41

1 Answer 1

2

You may fix your code like this:

Dim str
Set objRegExp = new RegExp
str = "one two alksdjfkl <b> sdklifjklsdjf </b> <b:FileName>Test</b:FileName> jsdhfj rutyier x,mcvn,mcx </b> <b:FileName>Test2222.docx</b:FileName> mvbn,cmvb eiurtyeiurty"
objRegExp.Global = True
objRegExp.Pattern = "<b:FileName>(.*?)</b:FileName>"
Set objMatches = objRegExp.Execute(str)
MsgBox objMatches.Count
For i = 0 To objMatches.Count - 1
    Set Match = objMatches.Item(i)
    MsgBox Match.SubMatches(0)
Next

NOTES

  • <b:FileName>(.*?)</b:FileName> is the regex that matches and captures any 0 or more chars other than line break chars between <b:FileName> and </b:FileName> into Group 1
  • To access the Group 1 value, use Match.SubMatches(0).
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.