MochiuWiki : SUSE, EC, PCB
案内
メインページ
最近の更新
おまかせ表示
MediaWiki についてのヘルプ
ツール
リンク元
関連ページの更新状況
特別ページ
ページ情報
We ask for
Donations
検索
個人用ツール
ログイン
Toggle dark mode
名前空間
ページ
議論
表示
閲覧
ソースを閲覧
履歴を表示
C Sharpの基礎 - 正規表現のソースを表示
提供: MochiuWiki : SUSE, EC, PCB
←
C Sharpの基礎 - 正規表現
あなたには「このページの編集」を行う権限がありません。理由は以下の通りです:
この操作は、次のグループのいずれかに属する利用者のみが実行できます:
管理者
、new-group。
このページのソースの閲覧やコピーができます。
== 概要 == C#において、Regexクラスの使用方法について記載する。<br> <br> Regexクラスには、IsMatch、Match、Matches、Replace等のメソッドが存在する。<br> 正規表現を使用して、入力文字列にマッチするかの判定や、マッチした文字列の置換等ができる。<br> <br><br> == IsMatch == C#では、IsMatchメソッドを使用して、パターンにマッチするか判定できる。<br> <syntaxhighlight lang="c#"> using System.Text.RegularExpressions; class Sample { static void Main() { string str = ""Hello world.""; // パターンにマッチするか判定 bool bResult1 = Regex.IsMatch(str, ""Hello""); bool bResult2 = Regex.IsMatch(str, ""[a-z]*""); bool bResult3 = Regex.IsMatch(str, ""[0-9]+""); System.Console.WriteLine(bResult1); System.Console.WriteLine(bResult2); System.Console.WriteLine(bResult3); System.Console.ReadKey(); } } // 出力 True True False </syntaxhighlight> <br><br> == Matchメソッド == C#では、Matchメソッドを使用して、検索文字列の先頭のパターンにマッチする文字列を抽出できる。<br> <syntaxhighlight lang="c#"> using System.Text.RegularExpressions; class Sample { static void Main() { string str = ""Hello world.""; // 最初にパターンにマッチする文字列の抽出 Match matche1 = Regex.Match(str, ""[a-zA-Z]+""); Match matche2 = Regex.Match(str, ""[a-z]""); System.Console.WriteLine(matche1.Value); System.Console.WriteLine(matche2.Value); System.Console.ReadKey(); } } // 出力 Hello e </syntaxhighlight> <br> また、Matchメソッドに<code>"RegexOptions.RightToLeft"</code>を指定することで、<br> 検索文字列の最後尾からパターンにマッチする文字列を抽出できる。<br> <syntaxhighlight lang="c#"> using System.Text.RegularExpressions; class Sample { static void Main() { string str = ""Hello world.""; // 最後にパターンにマッチする文字列の抽出 Match matche1 = Regex.Match(str, ""[a-zA-Z]+"", RegexOptions.RightToLeft); Match matche2 = Regex.Match(str, ""[a-z]"", RegexOptions.RightToLeft); System.Console.WriteLine(matche1.Value); System.Console.WriteLine(matche2.Value); System.Console.ReadKey(); } } // 出力 world d </syntaxhighlight> <br><br> == Matchesメソッド == C#では、Matchesメソッドを使用して、パターンにマッチする文字列を抽出できる。<br> <syntaxhighlight lang="c#"> using System.Text.RegularExpressions; class Sample { static void Main() { string str = ""Hello world.""; // パターンにマッチする文字列の抽出 MatchCollection matche = Regex.Matches(str, ""[a-e]""); // マッチした文字列の表示 foreach (Match m in matche) { System.Console.WriteLine(m.Value); } System.Console.ReadKey(); } } // 出力 e d </syntaxhighlight> <br><br> == Replaceメソッド == C#では、Replaceメソッドを使用して、パターンにマッチする文字列を置換できる。<br> <syntaxhighlight lang="c#"> using System.Text.RegularExpressions; class Sample { static void Main() { string str = ""Hello world.""; Regex regex = new Regex(""o""); // パターンにマッチする文字列をすべて置換 string result = regex.Replace(str, ""X""); System.Console.WriteLine(result); System.Console.ReadKey(); } } //出力 HellX wXrld. </syntaxhighlight> <br> また、パターンの最初にマッチする文字列のみを置換することもできる。<br> <syntaxhighlight lang="c#"> using System.Text.RegularExpressions; class Sample { static void Main() { string str = ""Hello world.""; Regex regex = new Regex(""o""); // パターンにマッチする最初の文字列を置換 string result = regex.Replace(str, ""X"", 1); System.Console.WriteLine(result); System.Console.ReadKey(); } } // 出力 HellX world. </syntaxhighlight> <br><br> == 応用 == パターンの最後尾にマッチする文字列のみを置換することもできる。<br> <syntaxhighlight lang="c#"> using System.Text.RegularExpressions; class Sample { static void Main() { string str = ""Hello world.""; // パターンにマッチする最後の文字列を置換 string result = ReplaceLast(str, ""o"", ""X""); System.Console.WriteLine(result); System.Console.ReadKey(); } public static string ReplaceLast(string str, string target, string replace) { int index = str.LastIndexOf(target); if(index == -1) { return str; } string result = str.Remove(index, target.Length).Insert(index, replace); return result; } } // 出力 Hello wXrld. </syntaxhighlight> <br><br> __FORCETOC__ [[カテゴリ:C_Sharp]]
C Sharpの基礎 - 正規表現
に戻る。
案内
メインページ
最近の更新
おまかせ表示
MediaWiki についてのヘルプ
ツール
リンク元
関連ページの更新状況
特別ページ
ページ情報
We ask for
Donations
Collapse