CRegExp.SubMatchesCountproperty
Returns the number of submatches.
Syntax
PROPERTY SubMatchesCount (BYVAL index AS LONG = 0) AS LONG
Parameters
| Name | Description | |
|---|---|---|
index | 0-based index of the match to retrieve. |
Description
Returns the number of submatches.
Examples
DIM pRegExp AS CRegExp DIM dwsText AS DWSTRING = "My cat found a dead bat over the mat" pRegExp.Pattern = $"\b[bcm]at\b" pRegExp.IgnoreCase = TRUE pRegExp.Global = TRUE '-or- pRegPattern = "\b(b|c|m)at\b" pRegExp.Execute(dwsText) FOR i AS LONG = 0 TO pRegExp.MatchesCount - 1 PRINT pRegExp.MatchValue(i) NEXT
pRegExp.Pattern = $"\b(b|c|m)at\b"
Output: cat bat mat
DIM pRegExp AS CRegExp DIM dwsText AS DWSTRING = "Hello, my name is John Doe, what's your name?" pRegExp.Pattern = $"\bJoh?n(athan)? Doe\b" pRegExp.IgnoreCase = TRUE pRegExp.Global = TRUE pRegExp.Execute(dwsText) IF pRegExp.MatchesCount THEN print pRegExp.MatchValue ' Output "John Doe"
DIM pRegExp AS CRegExp pRegExp.Global = TRUE pRegExp.Pattern = "[abc]" DIM dws AS DWSTRING = pRegExp.Remove("abacadabra") PRINT dws ' - prints "dr"
DIM pRegExp AS CRegExp pRegExp.Pattern = "ab" pRegExp.Global = TRUE DIM dws AS DWSTRING = pRegExp.Remove("abacadabra") PRINT dws 'Output: acadra
DIM pRegExp AS CRegExp pRegExp.IgnoreCase = TRUE pRegExp.Global = TRUE pRegExp.Pattern = $"\bworld\b" DIM dws AS DWSTRING = pRegExp.Remove("World, worldx, world") PRINT dws ' Output: ", worldx,"
DIM pRegExp AS CRegExp pRegExp.Global = TRUE pRegExp.Pattern = "[bac]" DIM dws AS DWSTRING = pRegExp.Replace("abacadabra", "") PRINT dws ' Output: "d**r*"
DIM pRegExp AS CRegExp pRegExp.Global = TRUE pRegExp.IgnoreCase = TRUE pRegExp.Pattern = "World" DIM dws AS DWSTRING = pRegExp.Replace("Hello World", "Earth") PRINT dws ' Output: "Hello Earth"
DIM pRegExp AS CRegExp DIM dwsText AS DWSTRING = "Is is the cost of of gasoline going up up?" pRegExp.Global = TRUE pRegExp.IgnoreCase = TRUE pRegExp.Pattern = $"\b([a-z]+) \1\b" DIM dws AS DWSTRING = pRegExp.Replace(dwsText, "$1") PRINT dws ' Output: "Is the cost of gasoline going up?"
DIM pRegExp AS CRegExp DIM dwsText AS DWSTRING = "This is a text.With some dots.Between words. This one not." pRegExp.Pattern = "(\.)(\w)" pRegExp.Global = TRUE pRegExp.IgnoreCase = TRUE DIM dws AS DWSTRING = pRegExp.Replace(dwsText, "$1 $2") PRINT dws ' Output: This is a text. With some dots. Between words. This one not."
DIM pRegExp AS CRegExp DIM dwsText AS DWSTRING = "555-123-4567" pRegExp.Pattern = "(\d{3})-(\d{3})-(\d{4})" pRegExp.Global = TRUE pRegExp.IgnoreCase = TRUE DIM dws AS DWSTRING = pRegExp.Replace(dwsText, "($1) $2-$3") PRINT dws ' Output: (555) 123-4567
DIM pRegExp AS CRegExp DIM dwsText AS DWSTRING = "0000.34500044" pRegExp.Pattern = $"\b0{1,}\." pRegExp.Global = TRUE pRegExp.IgnoreCase = TRUE DIM dws AS DWSTRING = pRegExp.Replace(dwsText, ".") print dws ' Output: ".34500044"
DIM pRegExp AS CRegExp DIM dwsText AS DWSTRING = "This is a ""quoted"" string" pRegExp.Pattern = """[^""]""" pRegExp.Global = TRUE DIM dws AS DWSTRING = pRegExp.Replace(dwsText, "") print dws ' Output: "This is a "*"" string"
DIM pRegExp AS CRegExp DIM dwsText AS DWSTRING = "This is a " & CHR(9) & " test string" 'DIM dwsText AS DWSTRING = !"This is a \t test string" ' alternative way pRegExp.Pattern = $"\t" pRegExp.Global = TRUE DIM dws AS DWSTRING = pRegExp.Replace(dwsText, "|") print dws ' Output: "This is a | test string"
DIM pRegExp AS CRegExp DIM dwsText AS DWSTRING = "blah blah a234 blah blah x345 blah blah" pRegExp.Pattern = "[a-z][0-9][0-9][0-9]" pRegExp.Global = TRUE DIM nPos AS LONG = pRegExp.Find(dwsText) PRINT nPos ' Output: 11
DIM pRegExp AS CRegExp DIM dwsText AS DWSTRING = "blah blah a234 blah blah x345 blah blah" pRegExp.Pattern = "[a-z][0-9][0-9][0-9]" pRegExp.Global = TRUE DIM dws AS DWSTRING = pRegExp.FindEx(dwsText) PRINT dws ' Output: 11,4; 26,4
DIM pRegExp AS CRegExp DIM dwsText AS DWSTRING = "This is a test string" pRegExp.Pattern = "test" pRegExp.Global = TRUE IF pRegExp.Execute(dwsText) THEN PRINT pRegExp.MatchValue ' Output: "test"
DIM pRegExp AS CRegExp DIM dwsText AS DWSTRING = "This is a test string" pRegExp.Pattern = "test" pRegExp.IgnoreCase = TRUE pRegExp.Global = FALSE IF pRegExp.Execute(dwsText) THEN PRINT pRegExp.MatchValue ' Output: "test"
DIM pRegExp AS CRegExp DIM dwsText AS DWSTRING = "This is a test string" pRegExp.Pattern = "is a test" pRegExp.IgnoreCase = FALSE pRegExp.Global = FALSE IF pRegExp.Execute(dwsText) THEN PRINT pRegExp.MatchValue ' Output: "is a test"
DIM pRegExp AS CRegExp DIM dwsText AS DWSTRING = "This is a test string" pRegExp.Pattern = "is a test" pRegExp.IgnoreCase = TRUE pRegExp.Global = FALSE IF pRegExp.Execute(dwsText) THEN PRINT pRegExp.MatchValue ' Output: "is a test"
DIM pRegExp AS CRegExp DIM dwsText AS DWSTRING = "This is a test testx string" pRegExp.Pattern = "test" pRegExp.IgnoreCase = FALSE pRegExp.Global = TRUE IF pRegExp.Execute(dwsText) THEN FOR i AS LONG = 0 TO pRegExp.MatchesCount
PRINT pRegExp.MatchValue(i)
NEXT END IF ' Output: ' test ' test
DIM pRegExp AS CRegExp DIM dwsText AS DWSTRING = "This is a test testx string" pRegExp.Pattern = "test" pRegExp.IgnoreCase = TRUE pRegExp.Global = TRUE IF pRegExp.Execute(dwsText) THEN FOR i AS LONG = 0 TO pRegExp.MatchesCount
PRINT pRegExp.MatchValue(i)
NEXT END IF ' Output: ' test ' test
DIM pRegExp AS CRegExp DIM dwsText AS DWSTRING = "This is a test testx string" pRegExp.Pattern = $"\bTest\b" pRegExp.IgnoreCase = TRUE pRegExp.Global = TRUE IF pRegExp.Execute(dwsText) THEN FOR i AS LONG = 0 TO pRegExp.MatchesCount
PRINT pRegExp.MatchValue(i)
NEXT END IF ' Output: "test"
DIM pRegExp AS CRegExp DIM dwsText AS DWSTRING = "I have a cat and a dog, because I love cats and dogs" pRegExp.Pattern = "c.t|d.g" pRegExp.IgnoreCase = TRUE pRegExp.Global = TRUE IF pRegExp.Execute(dwsText) THEN FOR i AS LONG = 0 TO pRegExp.MatchesCount - 1
PRINT pRegExp.MatchValue(i)
NEXT END IF ' Output: ' cat ' dog ' cat ' dog
DIM pRegExp AS CRegExp DIM dwsText AS DWSTRING = "I have a cat and a dog, because I love cats and dogs" pRegExp.Pattern = $"\bc.t\b|\bd.g\b" pRegExp.IgnoreCase = TRUE pRegExp.Global = TRUE IF pRegExp.Execute(dwsText) THEN FOR i AS LONG = 0 TO pRegExp.MatchesCount - 1
PRINT pRegExp.MatchValue(i)
NEXT END IF ' Output: ' cat ' dog
DIM pRegExp AS CRegExp DIM dwsText AS DWSTRING = "The contests will be in January, July and November" pRegExp.Pattern = $"\b(january|february|march|april|may|june|july|" & _
$"august|september|october|november|december)\b"
pRegExp.IgnoreCase = TRUE pRegExp.Global = TRUE IF pRegExp.Execute(dwsText) THEN FOR i AS LONG = 0 TO pRegExp.MatchesCount - 1
PRINT pRegExp.MatchValue(i)
NEXT END IF ' Output: ' January ' July ' November
DIM pRegExp AS CRegExp DIM dwsText AS DWSTRING = "29:Sep:2017 ""This is an Example!""" pRegExp.Pattern = """.*?""" pRegExp.IgnoreCase = TRUE pRegExp.Global = TRUE IF pRegExp.Execute(dwsText) THEN PRINT pRegExp.MatchValue ' Output: ' "This is an Example!"
DIM pRegExp AS CRegExp DIM dwsText AS DWSTRING = "29:Sep:2017 ""This is an Example!""" pRegExp.Pattern = "(?:[a-z][a-z]+)" pRegExp.IgnoreCase = TRUE pRegExp.Global = TRUE IF pRegExp.Execute(dwsText) THEN FOR i AS LONG = 0 TO pRegExp.MatchesCount - 1
PRINT pRegExp.MatchValue(i)
NEXT END IF ' Output: ' Sep ' This ' is ' an ' Example
DIM pRegExp AS CRegExp DIM dwsText AS DWSTRING = "29:Sep:2017 ""This is an Example!""" pRegExp.Pattern = $"((?:(?:[1]{1}\d{1}\d{1}\d{1})|(?:[2]{1}\d{3})))(?![\d])" IF pRegExp.Execute(dwsText) THEN PRINT pRegExp.MatchValue END IF ' Output: ' 2017
DIM pRegExp AS CRegExp DIM dwsText AS DWSTRING = "29:Sep:2017 ""This is an Example!""" pRegExp.Pattern = $"(\d+)" pRegExp.Global = TRUE IF pRegExp.Execute(dwsText) THEN FOR i AS LONG = 0 TO pRegExp.MatchesCount
PRINT pRegExp.MatchValue(i)
NEXT END IF ' Output: ' 29 ' 2017
' // extract the first match DIM pRegExp AS CRegExp DIM dwsText AS DWSTRING = "blah blah (text beween parentheses) blah blah" pRegExp.Pattern = $"([^(]*?)(?=\))" DIM dws AS DWSTRING = pRegExp.Extract(dwsText) PRINT dws
' // extract the first match after the 11th position DIM pRegExp AS CRegExp DIM dwsText AS DWSTRING = "blah (xxx) blah (text beween parentheses) blah blah" pRegExp.Pattern = $"([^(]*?)(?=\))" DIM dws AS DWSTRING = pRegExp.Extract(11, dwsText) PRINT dws
Output: "text between parentheses"
DIM pRegExp AS CRegExp DIM dwsText AS DWSTRING = "blah (xxx) blah {text beween curly braces} blah blah" pRegExp.Pattern = $"([^{]*?)(?=\})" DIM dws AS DWSTRING = pRegExp.Extract(dwsText) PRINT dws
DIM pRegExp AS CRegExp DIM dwsText AS DWSTRING = "blah (xxx) blah {text beween curly braces} blah blah" pRegExp.Pattern = $"([^{]*?)(?=\})" DIM dws AS DWSTRING = pRegExp.Extract(11, dwsText) PRINT dws
Output: "text between curly braces"
DIM pRegExp AS CRegExp DIM dwsText AS DWSTRING = "1.2345678901234567e+029" pRegExp.Pattern = $"^[\+\-]?\d*\.?\d+(?:[Ee][\+\-]?\d+)?$" PRINT pRegExp.Test(dwsText) ' Output: True
Pattern: "^[\+\-]?\d*\.?\d+(?:[Ee][\+\-]?\d+)?$"
DIM pRegExp AS CRegExp DIM dwsText AS DWSTRING = "https://www.google.es/search?q=msdn+jscript+check+if+an+url+is+valid&client=firefox-b&dcr=0&ei=hM_OWfb2BMzSU-mhr7AG&start=20&sa=N&biw=947&bih=394" pRegExp.Pattern = $"(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?" PRINT pRegExp.Test(dwsText) ' Output: True
DIM pRegExp AS CRegExp DIM dwsText AS DWSTRING = "http://msdn.microsoft.com:80/scripting/default.htm" pRegExp.Pattern = $"(\w+):\/\/([^/:]+)(:\d)?([^# ])" PRINT pRegExp.Execute(dwsText) FOR i AS LONG = 0 TO pRegExp.SubMatchesCount - 1 PRINT pRegExp.SubMatchValue(0, i) NEXT ' Output: ' http ' msdn.microsoft.com ' :80 ' /scripting/default.htm
Reference
- Include file
CRegExp.inc - Defined in AfxNova/CRegExp.inc:348
- Documented in Windows/Windows Script/CRegExp Class.md
- Topic: CRegExp Class