Created
October 3, 2016 21:42
-
-
Save vreemt/c136c270b9624ce0936c196018d2aad3 to your computer and use it in GitHub Desktop.
Notes on using regexes in C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://regex101.com/r/3v4pjk/1 | |
new Regex(@"^([A-Za-z]{2}(\d)(|\-))([A-Za-z]{3})-(\d)(\d)(\d)$)") | |
new Regex(@"^([A-Za-z]{2}(\d)(|\-))([A-Za-z]{3})-(\d)(\d)(\d)$)") | |
new Regex(@" | |
^ # start of line | |
[A-Za-z]{2} # two letters | |
\d # one digit | |
\- # hyphen | |
[A-Za-z]{3} # three letters | |
\- # hyphen | |
\d{3} # three digits | |
$ # end of line | |
)") | |
the pipe symbol before the first hyphen matches nothing or a hyphen - this means it will also match `aa6aaa-123` | |
a hyphen doesn't need escaping (escape = put a \ in front, so for e.g. the `\d` it doesn't match a d but instead looks for a single number) | |
new Regex(@"^[A-Za-z]{2}\d-[A-Za-z]{3}-\d{3}$") | |
Demo via https://regex101.com/r/3v4pjk/1 | |
http://www.mikesdotnetting.com/Article/46/c-regular-expressions-cheat-sheet | |
you don't need the pipe symbol | or brackets in there, but for clarity you can wrap the number/letter combinations in brackets, e.g. | |
new Regex(@"^([A-Za-z]{2}\d)-([A-Za-z]{3})-(\d{3})$") | |
capturing groups - matches[0].groups etc | |
each section in () is a capturing group | |
you can refer to these as \0 \1 etc | |
http://stackoverflow.com/questions/3665757/how-to-convert-char-to-int | |
int one = numbers[0]; | |
int a = (int)Char.GetNumericValue(numbers[0]); | |
Console.WriteLine(a); | |
int b = (int)Char.GetNumericValue(numbers[1]); | |
int cee = (int)Char.GetNumericValue(numbers[2]); | |
int d = numbers[3]; | |
int e = a + b + cee; | |
Console.WriteLine(e); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
all the numbers separately in groups:
^[A-Za-z]{2}(\d)-[A-Za-z]{3}-(\d)(\d)(\d)$