[C#] Regular Expression自學筆記| Mike's開發瘋 - - 點部落
文章推薦指數: 80 %
&p_cate=all)"; Match match = Regex.Match(categoryUrl, pattern); if (match.Success) { Group g=match.Groups[0]; categoryID = g.ToString(); }.
實戰使用c#Regex的筆記
說明:https://goo.gl/RsQHtp
線上驗證
https://regex101.com/
常用語法說明
特殊字元
\ :後面為特殊符號,e.g \\代表\ , \n代表換行,\(代表(
^ :字串開始
$:字串結尾
.:任何字元除了\n
開頭與結尾
(?=Pattern):前面為pattern ,e.g Patternabc
(?<=Pattern):後面為pattern ,e.g abcPattern
集合及字元
[abc]:字元集合,e.g[a-zA-Z] :英文大小寫
[^a-z]:非a-z,e.g ABCdefGH ==> [^a-z] ==>ABC與 GH
\d :數字,e.g ABC123DEF==>[\d]* ==>123
\D:非數字,e.g ABC123DEF==>[\D]*==>ABC與DEF
\s:一個空白字元
\S:非空白字元
\w:單詞字元(a-z,A-Z,0-9,_) ,e.gNewYork -->\w+==> New與York
\W:非單詞字元
出現次數
*:0~n次
+:1~n次
?: 0-1次
{n}:n次
{n,m}:n~m次
優先順序:
1. \
2. () (?=)..[]
3. *+? {n,m}
4.^$
5. |
範例1:取出夾在S1與S2字串中間的文字
/stexh/H000499?pa=000242102131000499&p_cate=all
欲取出:H000499?pa=000242102131000499
stringpattern=@"(?<=\/stexh\/)(.*)(?=&p_cate=all)";
Matchmatch=Regex.Match(categoryUrl,pattern);
if(match.Success)
{
Groupg=match.Groups[0];
categoryID=g.ToString();
}
範例2:取出金額數字
9折:特價100元
///
延伸文章資訊
- 1[C#] Regular Expression自學筆記| Mike's開發瘋 - - 點部落
&p_cate=all)"; Match match = Regex.Match(categoryUrl, pattern); if (match.Success) { Group g=matc...
- 2Guide to Regular Expressions and Matching Strings in JavaScript
- 3Python RegEx: re.match(), re.search(), re.findall() with Example
- 4Regex.Match 方法(System.Text.RegularExpressions)
在輸入字串搜尋規則運算式的項目,並傳回正確結果為單一Match 物件。 ... IgnoreCase); // Match the regular expression pattern agai...
- 5Regex Match - JMP
The second list is the text that matches backreference 1, and so on. Regex Match(source, pattern,...