대문자 2글자 추출
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
using System; using System.Text.RegularExpressions; class Program { static void Main() { // Part 1: the input string. string input = "abcABC1234567ㅏㅣ"; // Part 2: call Regex.Match. Match match = Regex.Match(input, @"[A-Z]{2}", RegexOptions.IgnoreCase); // Part 3: check the Match for Success. if (match.Success) { // Part 4: get the Group value and display it. string key = match.Groups[0].Value; Console.WriteLine(key); } } } //AB |
다음은 숫자만 추출할때 쓰면 된다.
반드시 숫자가 하나이상 있어야 됨.
1 2 3 4 5 6 7 |
using System.Text.RegularExpressions;</code> string strText = "abc1234567ㅏㅣ" string strNum = ""; strNum = Regex.Replace(strText, @"\D", ""); |
=> 1234567
닷넷 정규식에서 \d는 숫자. \D는 숫자가 아닌 문자를 의미
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
abc… Letters 123… Digits \d Any Digit \D Any Non-digit character . Any Character \. Period [abc] Only a, b, or c [^abc] Not a, b, nor c [a-z] Characters a to z [0-9] Numbers 0 to 9 \w Any Alphanumeric character \W Any Non-alphanumeric character {m} m Repetitions {m,n} m to n Repetitions * Zero or more repetitions + One or more repetitions ? Optional character \s Any Whitespace \S Any Non-whitespace character ^…$ Starts and ends (…) Capture Group (a(bc)) Capture Sub-group (.*) Capture all (abc|def) Matches abc or def |