-1

First of all I have read all the references that match the above Title but none seem to answer my simple requirement. I must use a regex pattern as I need to feed it to a third-party library for processing.

Part 1: The xml file to read from is a simple project.exe.config file. The regex that I have already used is

 <system.serviceModel>[\s\S]*?<\/system.serviceModel>

This reads the node and its contents however I ONLY need the CONTENTS to be returned.

Part 2: This should be so simple that I am embarrassed to ask in such an esteemed forum as this. Can you recommend a program that will a) allow me to test various regex patterns on the subject file and b) be designed to tech me Regex patterns at the same time. I've paid for and installed RegExBuddy but the program itself seems to confusing for me, let alone the regex itself.

Edit: VBscript does not support lookbehind. I need to use VBscript as I'm using third-party tools.

dinhit
  • 672
  • 1
  • 17
David
  • 958
  • 2
  • 12
  • 29
  • Try https://regex101.com – code Mar 23 '23 at 01:14
  • 1
    Does this answer your question? [RegEx match open tags except XHTML self-contained tags](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – Geert Bellekens Mar 23 '23 at 07:57

2 Answers2

-1

You can use lookarounds to only capture the content:

(?<=<system\.serviceModel>)[\s\S]*?(?=<\/system\.serviceModel>)

Try it.

code
  • 5,690
  • 4
  • 17
  • 39
-1

That can be done quite simple using positive lookbehind (?<=) and positive lookahead (?=)

(?<=<system.serviceModel>)[\s\S]*?(?=<\/system.serviceModel>)

Example: regex101

Positive lookbehind will look for match behind a specific pattern, in this case is <system.serviceModel> and vice versa, positive lookahead will look ahead <\/system.serviceModel> pattern.

To test and learn regex, regex101.com is a good place that supports many language, has a friendly UI with explaination, and also have a nice debugger

Edit: OP edited the required to use the regex with VBScript, which doesn't support lookbehind. So the afternative is using capturing group:

<system.serviceModel>([\s\S]*?)(?=<\/system.serviceModel>)

It also work without the lookahead

<system.serviceModel>([\s\S]*?)<\/system.serviceModel>

I'm not familiar with VBScript, but the concept will be something like this:

Set regexObj = CreateObject("vbscript.regexp")
regexObj.Pattern = "<system.serviceModel>([\s\S]*?)(?=<\/system.serviceModel>)"
' or <system.serviceModel>([\s\S]*?)<\/system.serviceModel>
Set matchesObj= regexObj.Execute("<system.serviceModel>Something in side the xml tag!</system.serviceModel>")
If matchesObj.Count <> 0 Then
    WScript.Echo(matchesObj(0).Submatches(0))
    ' or matchesObj.Item(0).SubMatches.Item(0) 
    ' to access first submatch group ([\s\S]*?)
End If
dinhit
  • 672
  • 1
  • 17