Having had to code the same regular expression code in 3 different languages today, this post shows how the syntax differs for a fairly simple regular expression task. I'm posting it here for so I can look it up iin teh future, and it
might be useful to someone out there - I'm simply looking for 2 parts of a frequency code string (in the format of [number][code] e.g. "15M"), which I want to capture and store in 2 variables...
VBScript code
Dim m As Match
Dim matches As MatchCollection
Dim re As RegExp
Set re = New RegExp
re.IgnoreCase = True
re.Global = True
re.Pattern = "(\d*)([DWMQY])"
Set matches = re.Execute(sStringToSearch)
If matches.Count > 0 Then
Set m = matches(0)
nIntervalNumber = CInt(m.SubMatches(0))
sIntervalCode = m.SubMatches(1)
End if
Javascript code
var regExpression = new RegExp("(\\d*)([DWMQY])", "gi");
var regExMatch = regExpression.exec(sStringToSearch);
if (regExMatch)
{
nIntervalNumber = regExMatch[1];
sIntervalCode = regExMatch[2];
}
c# Code
Regex regex = new Regex(@"(\d*)([DWMQY])", RegexOptions.IgnoreCase);
Match match = regex.Match(stringToSearch);
if (match.Success)
{
intervalNumber = Convert.ToInt32(match.Groups[1].ToString());
intervalCode = match.Groups[2].ToString();
}
.. so there you have it. Very boring code, but at least it shows the same code in different languages!