Tube
Tube

Reputation: 25

Get substring from specific string using vbscript

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!

Upvotes: 1

Views: 1349

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627507

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

Upvotes: 2

Related Questions