Matt Roberts

Rails and .NET

  Home  |   Contact  |   Syndication    |   Login
  37 Posts | 0 Stories | 33 Comments | 35 Trackbacks

News




Twitter












Archives

Post Categories

BlogRoll

Entropy

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!
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted on Thursday, June 01, 2006 9:52 AM

Feedback

# re: Regular Expressions in VB/Javascript/.NET 1/11/2008 7:56 AM T.R.Harihara Sudhan
Dear Coder,

Really very nice. Many thanks for this one.

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: