Skip to content

Instantly share code, notes, and snippets.

@whoo24
Created November 29, 2016 06:15
Show Gist options
  • Save whoo24/3c0be0edb06fadb0fe39a35db245aea5 to your computer and use it in GitHub Desktop.
Save whoo24/3c0be0edb06fadb0fe39a35db245aea5 to your computer and use it in GitHub Desktop.
Get decimal string in (0.0, 1.0, 2.0) using Regex.
using System;
using System.Text.RegularExpressions;
namespace regex {
class Program {
static void Main (string[] args) {
FindDecimalNumber();
FindCapturedDeciaml();
}
static void FindDecimalNumber () {
string text = "(0.0, 1.0, 2.0)";
string pattern = @"\d+\.?\d*";
Regex re = new Regex(pattern);
MatchCollection mc = re.Matches(text);
foreach (var m in mc) {
Console.WriteLine(m.ToString());
}
Console.WriteLine(mc.Count);
}
static void FindCapturedDeciaml () {
string text = "(0.0, 1.0, 2.0)";
string pattern = @"\((?<x>\d+\.?\d*), (?<y>\d+\.?\d*), (?<z>\d+\.?\d*)\)";
Regex re = new Regex(pattern);
Match m = re.Match(text);
Console.WriteLine(m.Groups["x"].ToString()); // 0.0
Console.WriteLine(m.Groups["y"].ToString()); // 1.0
Console.WriteLine(m.Groups["z"].ToString()); // 2.0
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment