Language
Note
ãã®ããŒãžã«ã¢ã¯ã»ã¹ããã«ã¯ãæ¿èªãå¿ èŠã§ãã ãµã€ã³ã€ã³ãŸãã¯ãã£ã¬ã¯ããªã®å€æŽã詊ãããšãã§ããŸãã
ãã®ããŒãžã«ã¢ã¯ã»ã¹ããã«ã¯ãæ¿èªãå¿ èŠã§ãã ãã£ã¬ã¯ããªã®å€æŽã詊ãããšãã§ããŸãã
æ¢å®ã§ã¯ãæ£èŠè¡šçŸãã¿ãŒã³ã§å ¥åæååãšãªãã©ã«æåãæ¯èŒããéã«ã倧æåãšå°æåãåºå¥ãããŸãããŸããæ£èŠè¡šçŸãã¿ãŒã³ã«ããã空çœã¯ããªãã©ã«ãªç©ºçœæåãšããŠè§£éãããŸããããã«ãæ£èŠè¡šçŸã§äœ¿çšããããã£ãã㣠ã°ã«ãŒãã¯ãæé»çã«ååãä»ããããå Žåãšæç€ºçã«ååãä»ããããå ŽåããããŸãã æ£èŠè¡šçŸã®ãªãã·ã§ã³ãæå®ããããšã§ããããã®æ£èŠè¡šçŸã®æ¢å®ã®åäœãšãã®ããã€ãã®åŽé¢ã倿Žã§ããŸãã ãããã®ãªãã·ã§ã³ã®äžéšã次ã®è¡šã«ç€ºããŸãããæ£èŠè¡šçŸãã¿ãŒã³ã®äžéšãšããŠã€ã³ã©ã€ã³ã§èšè¿°ããããšããSystem.Text.RegularExpressions.Regex ã¯ã©ã¹ ã³ã³ã¹ãã©ã¯ã¿ãŒãŸãã¯éçãã¿ãŒã³äžèŽã¡ãœããã« System.Text.RegularExpressions.RegexOptions åæå€ãšããŠæž¡ãããšãã§ããŸãã
RegexOptions ã®ã¡ã³ã㌠|
ã€ã³ã©ã€ã³æå | 广 | 詳现æ å ± |
|---|---|---|---|
| None | 䜿çšã§ããŸãã | æ¢å®ã®åäœã䜿çšããŸãã | æ¢å®ã®ãªãã·ã§ã³ |
| IgnoreCase | i |
倧æåãšå°æåãåºå¥ããªãäžèŽã䜿çšããŸãã | ã±ãŒã¹ã€ã³ã»ã³ã·ãã£ããããã³ã° |
| Multiline | m |
è€æ°è¡ã¢ãŒãã䜿çšããŸããããã§ã^ ãš $ ã¯ã(å
¥åæååã®å
é ããã³æ«å°Ÿã§ã¯ãªã) åè¡ã®å
é ããã³æ«å°Ÿã瀺ããŸãã |
è€æ°è¡ã¢ãŒã |
| Singleline | s |
åäžè¡ã¢ãŒãã䜿çšããŸãããã®ã¢ãŒãã§ã¯ãããªãªã (.) ã¯ä»»æã® 1 æåãšäžèŽããŸã (\n ãé€ããã¹ãŠã®æåã®ä»£çš)ã |
åäžè¡ã¢ãŒã |
| ExplicitCapture | n |
ååã®ãªãã°ã«ãŒãããã£ããã£ããŸããã
(?<
name>subexpression) ãšãã圢åŒã§ãæç€ºçã«ååãŸãã¯çªå·ãä»å ãããã°ã«ãŒãã®ã¿ãæå¹ãªãã£ããã£å¯Ÿè±¡ãšããŸãã |
æç€ºçãªãã£ããã£ã®ã¿ |
| Compiled | 䜿çšã§ããŸãã | æ£èŠè¡šçŸãã¢ã»ã³ããªã«ã³ã³ãã€ã«ããŸãã | ã³ã³ãã€ã«ãããæ£èŠè¡šçŸ |
| IgnorePatternWhitespace | x |
ãšã¹ã±ãŒããããŠããªã空çœããã¿ãŒã³ããé€å€ããã·ã£ãŒãèšå· (#) ã®åŸãã®ã³ã¡ã³ããæå¹ã«ããŸãã |
空çœãç¡èŠ |
| RightToLeft | 䜿çšã§ããŸãã | æ€çŽ¢ã®æ¹åã倿ŽããŸãã å·Šããå³ã§ã¯ãªããå³ããå·Šã«æ€çŽ¢ããŸãã | å³ããå·Šã¢ãŒã |
| ECMAScript | 䜿çšã§ããŸãã | åŒã® ECMAScript æºæ ã®åäœãæå¹ã«ããŸãã | ECMAScript äžèŽã®åäœ |
| CultureInvariant | 䜿çšã§ããŸãã | èšèªã®æåã®éããç¡èŠããã | ã€ã³ããªã¢ã³ã ã«ã«ãã£ã䜿çšããæ¯èŒ |
| NonBacktracking | 䜿çšã§ããŸãã | ããã¯ãã©ããã³ã°ãé¿ããå ¥åã®é·ãã«æ¯äŸããç·åœ¢æéã§åŠçã§ããã¢ãããŒãã䜿çšããŠäžèŽãããŸãã (.NET 7 以éã®ããŒãžã§ã³ã§äœ¿çšã§ããŸã)ã | éããã¯ãã©ããã³ã° ã¢ãŒã |
| AnyNewLine | 䜿çšã§ããŸãã |
^ã$ã\Zãããã³.ã¯\nã ãã§ãªãããã¹ãŠã®äžè¬çãªæ¹è¡ã·ãŒã±ã³ã¹ãèªèããŸãã (.NET 11 以éã®ããŒãžã§ã³ã§äœ¿çšã§ããŸã)ã |
AnyNewLine ã¢ãŒã |
ãªãã·ã§ã³ã®æå®
æ£èŠè¡šçŸã®ãªãã·ã§ã³ã¯ã次㮠3 çš®é¡ã®æ¹æ³ã®ããããã§æå®ã§ããŸãã
optionsã System.Text.RegularExpressions.Regex ãªã©ãSharedã¯ã©ã¹ ã³ã³ã¹ãã©ã¯ã¿ãŒãŸãã¯ãéç (Visual Basic ã®å Žå㯠Regex(String, RegexOptions)) ãã¿ãŒã³äžèŽã¡ãœããã® Regex.Match(String, String, RegexOptions) ãã©ã¡ãŒã¿ãŒã§æå®ããŸããoptionsãã©ã¡ãŒã¿ãŒã¯ãSystem.Text.RegularExpressions.RegexOptions åæå€ã®ãããããšã® OR ã®çµã¿åããã§ããã¯ã©ã¹ ã³ã³ã¹ãã©ã¯ã¿ãŒã® Regex ãã©ã¡ãŒã¿ãŒã䜿çšããŠããªãã·ã§ã³ã
optionsã€ã³ã¹ã¿ã³ã¹ã«æå®ããããšããããã®ãªãã·ã§ã³ã¯ System.Text.RegularExpressions.RegexOptions ããããã£ã«å²ãåœãŠãããŸãã ãã ããSystem.Text.RegularExpressions.RegexOptions ããããã£ã¯ãæ£èŠè¡šçŸãã¿ãŒã³èªäœã§ã®ã€ã³ã©ã€ã³ ãªãã·ã§ã³ãåæ ããŸãããå ·äœçãªäŸã次ã«ç€ºããŸãã
optionsã¡ãœããã® Regex.Match(String, String, RegexOptions) ãã©ã¡ãŒã¿ãŒã䜿çšããŠãæå "d" ã§å§ãŸãåèªãèå¥ãããšãã«ã倧æåãšå°æåãåºå¥ããªãäžèŽãæå¹ã«ãããšåæã«ããã¿ãŒã³ã®ç©ºçœãç¡èŠããŸããstring pattern = @"d \w+ \s"; string input = "Dogs are decidedly good pets."; RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace; foreach (Match match in Regex.Matches(input, pattern, options)) Console.WriteLine($"'{match.Value}// found at index {match.Index}."); // The example displays the following output: // 'Dogs // found at index 0. // 'decidedly // found at index 9.Dim pattern As String = "d \w+ \s" Dim input As String = "Dogs are decidedly good pets." Dim options As RegexOptions = RegexOptions.IgnoreCase Or RegexOptions.IgnorePatternWhitespace For Each match As Match In Regex.Matches(input, pattern, options) Console.WriteLine("'{0}' found at index {1}.", match.Value, match.Index) Next ' The example displays the following output: ' 'Dogs ' found at index 0. ' 'decidedly ' found at index 9.(?imnsx-imnsx)ãšããæ§æã§ãã€ã³ã©ã€ã³ ãªãã·ã§ã³ãæ£èŠè¡šçŸãã¿ãŒã³ã«é©çšããŸãã ãã®å Žåããªãã·ã§ã³ãå®çŸ©ãããŠãããã¿ãŒã³ã®å é ããããã¿ãŒã³ã®æ«å°Ÿãå«ããã€ã³ããŸã§ããŸãã¯å¥ã®ã€ã³ã©ã€ã³ ãªãã·ã§ã³ã§ãªãã·ã§ã³ãå®çŸ©ãããŠããªããã€ã³ããŸã§ãç¯å²ãšããŠããªãã·ã§ã³ããã¿ãŒã³ã«é©çšãããŸãã System.Text.RegularExpressions.RegexOptions ã€ã³ã¹ã¿ã³ã¹ã® Regex ããããã£ã¯ããããã®ã€ã³ã©ã€ã³ ãªãã·ã§ã³ãåæ ããªãããšã«æ³šæããŠãã ããã 詳ããã¯ãããã®ä»ã®æ§æäœãã®ãããã¯ãã芧ãã ãããå ·äœçãªäŸã次ã«ç€ºããŸãã ã€ã³ã©ã€ã³ ãªãã·ã§ã³ã䜿çšããŠãæå "d" ã§å§ãŸãåèªãèå¥ãããšãã«ã倧æåãšå°æåãåºå¥ããªãäžèŽãæå¹ã«ãããšåæã«ããã¿ãŒã³ã®ç©ºçœãç¡èŠããŸãã
string pattern = @"(?ix) d \w+ \s"; string input = "Dogs are decidedly good pets."; foreach (Match match in Regex.Matches(input, pattern)) Console.WriteLine($"'{match.Value}// found at index {match.Index}."); // The example displays the following output: // 'Dogs // found at index 0. // 'decidedly // found at index 9.Dim pattern As String = "\b(?ix) d \w+ \s" Dim input As String = "Dogs are decidedly good pets." For Each match As Match In Regex.Matches(input, pattern) Console.WriteLine("'{0}' found at index {1}.", match.Value, match.Index) Next ' The example displays the following output: ' 'Dogs ' found at index 0. ' 'decidedly ' found at index 9.(?imnsx-imnsx:subexpression)ãšããæ§æã§ãç¹å®ã®ã°ã«ãŒãåæ§æäœã®ã€ã³ã©ã€ã³ ãªãã·ã§ã³ãæ£èŠè¡šçŸãã¿ãŒã³ã«é©çšããŸãã ãªãã·ã§ã³ã»ããã®åã«èšå·ããªãå Žåã¯ãã®ã»ããããªã³ã«ãªãããã€ãã¹èšå·ãä»ããŠããå Žåã¯ãã®ã»ããããªãã«ãªããŸãã (?ã¯èšèªæ§æèŠçŽ ã®æ§æã®åºå®éšåã§ããããªãã·ã§ã³ãæå¹ã§ãããç¡å¹ã§ãããã«ããããããå¿ èŠã«ãªããŸã)ããªãã·ã§ã³ã¯ããã®ã°ã«ãŒãã«å¯ŸããŠã®ã¿é©çšãããŸãã 詳ããã¯ããæ£èŠè¡šçŸã§ã®ã°ã«ãŒãåæ§æäœããã芧ãã ãããå ·äœçãªäŸã次ã«ç€ºããŸãã ã°ã«ãŒãåæ§æäœã®ã€ã³ã©ã€ã³ ãªãã·ã§ã³ã䜿çšããŠãæå "d" ã§å§ãŸãåèªãèå¥ãããšãã«ã倧æåãšå°æåãåºå¥ããªãäžèŽãæå¹ã«ãããšåæã«ããã¿ãŒã³ã®ç©ºçœãç¡èŠããŸãã
string pattern = @"\b(?ix: d \w+)\s"; string input = "Dogs are decidedly good pets."; foreach (Match match in Regex.Matches(input, pattern)) Console.WriteLine($"'{match.Value}// found at index {match.Index}."); // The example displays the following output: // 'Dogs // found at index 0. // 'decidedly // found at index 9.Dim pattern As String = "\b(?ix: d \w+)\s" Dim input As String = "Dogs are decidedly good pets." For Each match As Match In Regex.Matches(input, pattern) Console.WriteLine("'{0}' found at index {1}.", match.Value, match.Index) Next ' The example displays the following output: ' 'Dogs ' found at index 0. ' 'decidedly ' found at index 9.
ãªãã·ã§ã³ãã€ã³ã©ã€ã³ã§æå®ããå Žåã¯ããªãã·ã§ã³ãŸãã¯ãªãã·ã§ã³ ã»ããã®åã«ãã€ãã¹èšå· (-) ãä»ãããšããã®ãªãã·ã§ã³ã¯ãªãã«ãªããŸãã ããšãã°ãã€ã³ã©ã€ã³æ§æäœ (?ix-ms) 㯠RegexOptions.IgnoreCase ãªãã·ã§ã³ããã³ RegexOptions.IgnorePatternWhitespace ãªãã·ã§ã³ããªã³ã«ããRegexOptions.Multiline ãªãã·ã§ã³ããã³ RegexOptions.Singleline ãªãã·ã§ã³ããªãã«ããŸãã æ¢å®ã§ã¯ããã¹ãŠã®æ£èŠè¡šçŸã®ãªãã·ã§ã³ããªãã§ãã
泚æ
ã³ã³ã¹ãã©ã¯ã¿ãŒãŸãã¯ã¡ãœããåŒã³åºãã® options ãã©ã¡ãŒã¿ãŒã§æå®ããæ£èŠè¡šçŸãªãã·ã§ã³ãæ£èŠè¡šçŸãã¿ãŒã³ã®ã€ã³ã©ã€ã³ã§æå®ãããªãã·ã§ã³ãšç«¶åããå Žåã¯ãã€ã³ã©ã€ã³ã§æå®ãããªãã·ã§ã³ã䜿çšãããŸãã
次ã«ç€ºã 5 çš®é¡ã®æ£èŠè¡šçŸãªãã·ã§ã³ã¯ãoptions ãã©ã¡ãŒã¿ãŒãšã€ã³ã©ã€ã³ã®äž¡æ¹ã§èšå®ã§ããŸãã
次㮠7 ã€ã®æ£èŠè¡šçŸãªãã·ã§ã³ã¯ã options ãã©ã¡ãŒã¿ãŒã䜿çšããŠèšå®ã§ããŸãããã€ã³ã©ã€ã³ã§èšå®ããããšã¯ã§ããŸããã
ãªãã·ã§ã³ã®å€å¥
ã€ã³ã¹ã¿ã³ã¹åããããšãã« Regex ãªããžã§ã¯ãã«æž¡ããããªãã·ã§ã³ã®çš®é¡ã確èªããã«ã¯ãèªã¿åãå°çšã® Regex.Options ããããã£ã®å€ãååŸããŸãã
RegexOptions.None ãé€ããªãã·ã§ã³ãååšãããã©ããããã¹ãããã«ã¯ãç®çã® Regex.Options ããããã£ã®å€ãš RegexOptions ã®å€ã䜿çšã㊠AND æŒç®ãå®è¡ããŸãã æ¬¡ã«ããã®çµæã RegexOptions ã®å€ãšçãããã©ããããã¹ãããŸãã æ¬¡ã®äŸã¯ãRegexOptions.IgnoreCase ãªãã·ã§ã³ãèšå®ãããŠãããã©ããããã¹ãããŸãã
if ((rgx.Options & RegexOptions.IgnoreCase) == RegexOptions.IgnoreCase)
Console.WriteLine("Case-insensitive pattern comparison.");
else
Console.WriteLine("Case-sensitive pattern comparison.");
If (rgx.Options And RegexOptions.IgnoreCase) = RegexOptions.IgnoreCase Then
Console.WriteLine("Case-insensitive pattern comparison.")
Else
Console.WriteLine("Case-sensitive pattern comparison.")
End If
RegexOptions.None ããã¹ãããã«ã¯ã次ã®äŸã«ç€ºãããã«ãRegex.Options ããããã£ã®å€ã RegexOptions.None ãšçãããã©ããã確èªããŸãã
if (rgx.Options == RegexOptions.None)
Console.WriteLine("No options have been set.");
If rgx.Options = RegexOptions.None Then
Console.WriteLine("No options have been set.")
End If
次ã®ã»ã¯ã·ã§ã³ã§ã¯ã.NETã®æ£èŠè¡šçŸã§ãµããŒãããããªãã·ã§ã³ã®äžèЧã瀺ããŸãã
æ¢å®ã®ãªãã·ã§ã³
RegexOptions.None ãªãã·ã§ã³ã¯ããªãã·ã§ã³ãæå®ãããŠããªãããšã瀺ããŸããæ£èŠè¡šçŸãšã³ãžã³ã¯ããã®ãªãã·ã§ã³ã®æ¢å®ã®åäœã䜿çšããŸãã æ¬¡ã«äŸã瀺ããŸãã
ãã®ãã¿ãŒã³ã¯ãECMAScript æ£èŠè¡šçŸã§ã¯ãªããæšæºã®åœ¢åŒãšããŠè§£éãããŸãã
æ£èŠè¡šçŸãã¿ãŒã³ã¯ãå·Šããå³ã«å ¥åæååãšç §åãããŸãã
æ¯èŒã§ã¯å€§æåãšå°æåãåºå¥ãããŸãã
^ããã³$èšèªèŠçŽ ã¯ãå ¥åæååã®å é ããã³æ«å°Ÿã瀺ããŸãã å ¥åæååã®æ«å°Ÿã¯ãæ«å°Ÿã®æ¹è¡æå\nã§ãããŸããŸããã.èšèªèŠçŽ ã¯ã\nãé€ãä»»æã® 1 æåãšäžèŽããŸããæ£èŠè¡šçŸãã¿ãŒã³ã«å«ãŸãã空çœã¯ããªãã©ã«ã®ç©ºçœæåãšããŠè§£éãããŸãã
ãã¿ãŒã³ãå ¥åæååãšæ¯èŒãããšãã«ãçŸåšã®ã«ã«ãã£ã®èŠåã䜿çšãããŸãã
æ£èŠè¡šçŸãã¿ãŒã³ã®ãã£ãã㣠ã°ã«ãŒãã¯ãæé»çã«æå®ãããå Žåãšæç€ºçã«æå®ãããå ŽåããããŸãã
泚æ
RegexOptions.None ãªãã·ã§ã³ã«ã¯ãç䟡ãªã€ã³ã©ã€ã³ ãªãã·ã§ã³ã¯ååšããŸããã æ£èŠè¡šçŸãªãã·ã§ã³ãã€ã³ã©ã€ã³ã§é©çšããããšãã«ãç¹å®ã®ãªãã·ã§ã³ããªãã«ãããšãæ¢å®ã®åäœããªãã·ã§ã³ããšã«åŸ©å
ãããŸãã ããšãã°ã(?i) ã¯å€§æåãšå°æåãåºå¥ããªãæ¯èŒããªã³ã«ãã(?-i) ã¯æ¢å®ã®åäœ (倧æåãšå°æåãåºå¥ããæ¯èŒ) ã埩å
ããŸãã
RegexOptions.None ãªãã·ã§ã³ã¯æ£èŠè¡šçŸãšã³ãžã³ã®æ¢å®ã®åäœã衚ããŠããã®ã§ãã¡ãœããåŒã³åºãã§æç€ºçã«æå®ãããããšã¯ã»ãšãã©ãããŸããã 代ããã«ãoptions ãã©ã¡ãŒã¿ãŒã䜿çšããã«ã³ã³ã¹ãã©ã¯ã¿ãŒãŸãã¯éçãã¿ãŒã³äžèŽã¡ãœãããåŒã³åºãããŸãã
倧æåãšå°æåãåºå¥ããªãäžèŽ
IgnoreCase ãªãã·ã§ã³ãŸã㯠i ã€ã³ã©ã€ã³ ãªãã·ã§ã³ãæå®ãããšã倧æåãšå°æåãåºå¥ããªãäžèŽãå®è¡ãããŸãã æ¢å®ã§ã¯ãçŸåšã®ã«ã«ãã£ã®å€§æåãšå°æåã®è¡šèšèŠåã䜿çšãããŸãã
次ã®äŸã§ã¯ã"the" ã§å§ãŸããã¹ãŠã®åèªãšäžèŽããæ£èŠè¡šçŸãã¿ãŒã³ \bthe\w*\b ãå®çŸ©ããŸãã
Match ã¡ãœããã®æåã®åŒã³åºãã§ã¯æ¢å®ã®å€§æåãšå°æåãåºå¥ããæ¯èŒã䜿çšããŠããã®ã§ãçµæã®åºåãããæã®å
é ã®æåå "The" ã¯äžèŽãšããŠåŠçãããŠããªãããšãããããŸãã ãããäžèŽãšããŠåŠçãããã®ã¯ããªãã·ã§ã³ã Match ã«èšå®ã㊠IgnoreCase ã¡ãœãããåŒã³åºãããå Žåã§ãã
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"\bthe\w*\b";
string input = "The man then told them about that event.";
foreach (Match match in Regex.Matches(input, pattern))
Console.WriteLine($"Found {match.Value} at index {match.Index}.");
Console.WriteLine();
foreach (Match match in Regex.Matches(input, pattern,
RegexOptions.IgnoreCase))
Console.WriteLine($"Found {match.Value} at index {match.Index}.");
}
}
// The example displays the following output:
// Found then at index 8.
// Found them at index 18.
//
// Found The at index 0.
// Found then at index 8.
// Found them at index 18.
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim pattern As String = "\bthe\w*\b"
Dim input As String = "The man then told them about that event."
For Each match As Match In Regex.Matches(input, pattern)
Console.WriteLine("Found {0} at index {1}.", match.Value, match.Index)
Next
Console.WriteLine()
For Each match As Match In Regex.Matches(input, pattern, _
RegexOptions.IgnoreCase)
Console.WriteLine("Found {0} at index {1}.", match.Value, match.Index)
Next
End Sub
End Module
' The example displays the following output:
' Found then at index 8.
' Found them at index 18.
'
' Found The at index 0.
' Found then at index 8.
' Found them at index 18.
次ã®äŸã§ã¯ãåã®äŸã®æ£èŠè¡šçŸãã¿ãŒã³ã倿Žããoptions ãã©ã¡ãŒã¿ãŒã䜿çšãã代ããã«ãã€ã³ã©ã€ã³ ãªãã·ã§ã³ã䜿çšããŠã倧æåãšå°æåãåºå¥ããªãæ¯èŒãè¡ã£ãŠããŸãã æåã®ãã¿ãŒã³ã§ã¯ãæåå "the" ã®æå "t" ã®ã¿ã«é©çšããããããã°ã«ãŒãåæ§æäœã®å€§æåãšå°æåãåºå¥ããªããªãã·ã§ã³ãå®çŸ©ããŠããŸãã ãªãã·ã§ã³ã®æ§æäœããã¿ãŒã³ã®å
é ã«ããã®ã§ã2 çªç®ã®ãã¿ãŒã³ã§ã¯ã倧æåãšå°æåãåºå¥ããªããªãã·ã§ã³ãæ£èŠè¡šçŸå
šäœã«é©çšãããŠããŸãã
using System;
using System.Text.RegularExpressions;
public class CaseExample
{
public static void Main()
{
string pattern = @"\b(?i:t)he\w*\b";
string input = "The man then told them about that event.";
foreach (Match match in Regex.Matches(input, pattern))
Console.WriteLine($"Found {match.Value} at index {match.Index}.");
Console.WriteLine();
pattern = @"(?i)\bthe\w*\b";
foreach (Match match in Regex.Matches(input, pattern,
RegexOptions.IgnoreCase))
Console.WriteLine($"Found {match.Value} at index {match.Index}.");
}
}
// The example displays the following output:
// Found The at index 0.
// Found then at index 8.
// Found them at index 18.
//
// Found The at index 0.
// Found then at index 8.
// Found them at index 18.
Imports System.Text.RegularExpressions
Module CaseExample
Public Sub Main()
Dim pattern As String = "\b(?i:t)he\w*\b"
Dim input As String = "The man then told them about that event."
For Each match As Match In Regex.Matches(input, pattern)
Console.WriteLine("Found {0} at index {1}.", match.Value, match.Index)
Next
Console.WriteLine()
pattern = "(?i)\bthe\w*\b"
For Each match As Match In Regex.Matches(input, pattern)
Console.WriteLine("Found {0} at index {1}.", match.Value, match.Index)
Next
End Sub
End Module
' The example displays the following output:
' Found The at index 0.
' Found then at index 8.
' Found them at index 18.
'
' Found The at index 0.
' Found then at index 8.
' Found them at index 18.
è€æ°è¡ã¢ãŒã
RegexOptions.Multiline ãªãã·ã§ã³ãŸã㯠m ã€ã³ã©ã€ã³ ãªãã·ã§ã³ãæå®ãããšãæ£èŠè¡šçŸãšã³ãžã³ã§ã¯ãè€æ°è¡ã§æ§æãããå
¥åæååã®åŠçãæå¹ã«ãªããŸãã
^ ããã³ $ èšèªèŠçŽ ã®è§£éã倿ŽããŠãããããå
¥åæååã®å
é ããã³æ«å°Ÿã§ã¯ãªããè¡ã®å
é ããã³æ«å°Ÿã瀺ãããã«ããŸãã
æ¢å®ã§ã¯ã$ ã¯å
¥åæååã®æ«å°Ÿã§ã®ã¿æ¡ä»¶ãæºããããŸãã
RegexOptions.Multiline ãªãã·ã§ã³ãæå®ããå Žåã¯ãæ¹è¡æå (\n) ãŸãã¯å
¥åæååã®æ«å°Ÿã®ããããã«ãã£ãŠæ¡ä»¶ãæºããããŸãã
ã©ã¡ãã®å Žåãã$ ã§ã¯åŸ©åž°æåãšæ¹è¡æåã®çµã¿åãã (\r\n) ã¯èªèãããŸããã
$ ã¯åžžã«ãã£ãªããžãªã¿ãŒã³ (\r) ãç¡èŠããŸãã
\r\n ãŸã㯠\n ã®ããããã§ç
§åãçµäºãããã«ã¯ã\r?$ ã§ã¯ãªããéšååŒ $ ã䜿çšããŸãã ããã«ããã\r éšåãäžèŽããããšã«ãªãããšã«æ³šæããŠãã ããã
ãã³ã
.NET 11 以éã§ã¯ã RegexOptions.AnyNewLine ã䜿çšãããšã^ã$ã\Zãããã³ . ã¯ã\nã§ã¯ãªããã¹ãŠã®äžè¬çãªæ¹è¡ã·ãŒã±ã³ã¹ãèªèã§ããããã\r? åé¿çã®å¿
èŠæ§ãæé€ã§ããŸãã
AnyNewLine 㯠\r\n ãã¢ãããã¯æ¹è¡ã·ãŒã±ã³ã¹ãšããŠæ±ãããã\r ãäžèŽã«å«ãŸããããšã¯ãããŸããã 詳现ã«ã€ããŠã¯ãã AnyNewLine ã¢ãŒã ãã»ã¯ã·ã§ã³ãåç
§ããŠãã ããã
次ã®äŸã§ã¯ãããŠãªã³ã°åå è
ã®ååãšã¹ã³ã¢ãæœåºããéé ã«äžŠã¹æ¿ããŠãSortedList<TKey,TValue> ã³ã¬ã¯ã·ã§ã³ã«è¿œå ããŠããŸãã
Matches ã¡ãœãã㯠2 ååŒã³åºãããŠããŸãã æåã®ã¡ãœããåŒã³åºãã§ã¯ã^(\w+)\s(\d+)$ ãšããæ£èŠè¡šçŸã䜿çšããããªãã·ã§ã³ã¯èšå®ãããŠããŸããã åºåçµæã瀺ãããã«ãæ£èŠè¡šçŸãšã³ãžã³ã¯å
¥åãã¿ãŒã³ãå
¥åæååã®å
é ããã³æ«å°ŸãšäžèŽãããããšãã§ããªãã®ã§ãäžèŽã¯æ€åºãããŸããã 2 çªç®ã®ã¡ãœããåŒã³åºãã§ã¯ãæ£èŠè¡šçŸã¯ ^(\w+)\s(\d+)\r?$ ã«å€æŽãããŠããããªãã·ã§ã³ã¯ RegexOptions.Multiline ã«èšå®ãããŠããŸãã åºåçµæã瀺ãããã«ãååãšã¹ã³ã¢ã®ç
§åã¯æ£åžžã«è¡ãããã¹ã³ã¢ã¯éé ã§è¡šç€ºãããŠããŸãã
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public class Multiline1Example
{
public static void Main()
{
SortedList<int, string> scores = new SortedList<int, string>(new DescendingComparer1<int>());
string input = "Joe 164\n" +
"Sam 208\n" +
"Allison 211\n" +
"Gwen 171\n";
string pattern = @"^(\w+)\s(\d+)$";
bool matched = false;
Console.WriteLine("Without Multiline option:");
foreach (Match match in Regex.Matches(input, pattern))
{
scores.Add(Int32.Parse(match.Groups[2].Value), (string)match.Groups[1].Value);
matched = true;
}
if (!matched)
Console.WriteLine(" No matches.");
Console.WriteLine();
// Redefine pattern to handle multiple lines.
pattern = @"^(\w+)\s(\d+)\r*$";
Console.WriteLine("With multiline option:");
foreach (Match match in Regex.Matches(input, pattern, RegexOptions.Multiline))
scores.Add(Int32.Parse(match.Groups[2].Value), (string)match.Groups[1].Value);
// List scores in descending order.
foreach (KeyValuePair<int, string> score in scores)
Console.WriteLine($"{score.Value}: {score.Key}");
}
}
public class DescendingComparer1<T> : IComparer<T>
{
public int Compare(T x, T y)
{
return Comparer<T>.Default.Compare(x, y) * -1;
}
}
// The example displays the following output:
// Without Multiline option:
// No matches.
//
// With multiline option:
// Allison: 211
// Sam: 208
// Gwen: 171
// Joe: 164
Imports System.Collections.Generic
Imports System.Text.RegularExpressions
Module Multiline1Example
Public Sub Main()
Dim scores As New SortedList(Of Integer, String)(New DescendingComparer1(Of Integer)())
Dim input As String = "Joe 164" + vbCrLf +
"Sam 208" + vbCrLf +
"Allison 211" + vbCrLf +
"Gwen 171" + vbCrLf
Dim pattern As String = "^(\w+)\s(\d+)$"
Dim matched As Boolean = False
Console.WriteLine("Without Multiline option:")
For Each match As Match In Regex.Matches(input, pattern)
scores.Add(CInt(match.Groups(2).Value), match.Groups(1).Value)
matched = True
Next
If Not matched Then Console.WriteLine(" No matches.")
Console.WriteLine()
' Redefine pattern to handle multiple lines.
pattern = "^(\w+)\s(\d+)\r*$"
Console.WriteLine("With multiline option:")
For Each match As Match In Regex.Matches(input, pattern, RegexOptions.Multiline)
scores.Add(CInt(match.Groups(2).Value), match.Groups(1).Value)
Next
' List scores in descending order.
For Each score As KeyValuePair(Of Integer, String) In scores
Console.WriteLine("{0}: {1}", score.Value, score.Key)
Next
End Sub
End Module
Public Class DescendingComparer1(Of T) : Implements IComparer(Of T)
Public Function Compare(x As T, y As T) As Integer _
Implements IComparer(Of T).Compare
Return Comparer(Of T).Default.Compare(x, y) * -1
End Function
End Class
' The example displays the following output:
' Without Multiline option:
' No matches.
'
' With multiline option:
' Allison: 211
' Sam: 208
' Gwen: 171
' Joe: 164
æ£èŠè¡šçŸãã¿ãŒã³ ^(\w+)\s(\d+)\r*$ ã¯ã次ã®è¡šã«ç€ºãããã«å®çŸ©ãããŠããŸãã
| ãã¿ãŒã³ | 説æ |
|---|---|
^ |
è¡ã®å é ããå§ããã |
(\w+) |
1 ã€ä»¥äžã®åèªæåã«äžèŽããŸãã ãããæåã®ãã£ãã㣠ã°ã«ãŒãã§ãã |
\s |
ç©ºçœæåãšäžèŽããŸãã |
(\d+) |
1 å以äžã® 10 鲿°ãšäžèŽããŸãã ããã 2 çªç®ã®ãã£ãã㣠ã°ã«ãŒãã§ãã |
\r? |
0 åãŸã㯠1 åã®åŸ©åž°æåãšäžèŽããŸãã |
$ |
è¡ã®æ«å°Ÿã§çµäºããŸãã |
次ã®äŸã¯ãåã®äŸãšç䟡ã§ãããã€ã³ã©ã€ã³ ãªãã·ã§ã³ (?m) ã䜿çšããŠè€æ°è¡ãªãã·ã§ã³ãèšå®ããŠããç¹ãç°ãªããŸãã
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public class Multiline2Example
{
public static void Main()
{
SortedList<int, string> scores = new SortedList<int, string>(new DescendingComparer<int>());
string input = "Joe 164\n" +
"Sam 208\n" +
"Allison 211\n" +
"Gwen 171\n";
string pattern = @"(?m)^(\w+)\s(\d+)\r*$";
foreach (Match match in Regex.Matches(input, pattern, RegexOptions.Multiline))
scores.Add(Convert.ToInt32(match.Groups[2].Value), match.Groups[1].Value);
// List scores in descending order.
foreach (KeyValuePair<int, string> score in scores)
Console.WriteLine($"{score.Value}: {score.Key}");
}
}
public class DescendingComparer<T> : IComparer<T>
{
public int Compare(T x, T y)
{
return Comparer<T>.Default.Compare(x, y) * -1;
}
}
// The example displays the following output:
// Allison: 211
// Sam: 208
// Gwen: 171
// Joe: 164
Imports System.Collections.Generic
Imports System.Text.RegularExpressions
Module Multiline2Example
Public Sub Main()
Dim scores As New SortedList(Of Integer, String)(New DescendingComparer(Of Integer)())
Dim input As String = "Joe 164" + vbCrLf +
"Sam 208" + vbCrLf +
"Allison 211" + vbCrLf +
"Gwen 171" + vbCrLf
Dim pattern As String = "(?m)^(\w+)\s(\d+)\r*$"
For Each match As Match In Regex.Matches(input, pattern, RegexOptions.Multiline)
scores.Add(CInt(match.Groups(2).Value), match.Groups(1).Value)
Next
' List scores in descending order.
For Each score As KeyValuePair(Of Integer, String) In scores
Console.WriteLine("{0}: {1}", score.Value, score.Key)
Next
End Sub
End Module
Public Class DescendingComparer(Of T) : Implements IComparer(Of T)
Public Function Compare(x As T, y As T) As Integer _
Implements IComparer(Of T).Compare
Return Comparer(Of T).Default.Compare(x, y) * -1
End Function
End Class
' The example displays the following output:
' Allison: 211
' Sam: 208
' Gwen: 171
' Joe: 164
åäžè¡ã¢ãŒã
RegexOptions.Singleline ãªãã·ã§ã³ãŸã㯠s ã€ã³ã©ã€ã³ ãªãã·ã§ã³ãæå®ãããšãæ£èŠè¡šçŸãšã³ãžã³ã§ã¯ãå
¥åæååãåäžè¡ã§æ§æãããŠãããã®ããã«åŠçãããŸãã ããªãªã (.) èšèªèŠçŽ ã®åäœã倿ŽããŠãæ¹è¡æå (\n) ãé€ããã¹ãŠã®æåã«äžèŽããã®ã§ã¯ãªããæ¹è¡æåãå«ãã©ã®æåã«ã§ãäžèŽããããã«ããŸãã
. ãªãã·ã§ã³ã䜿çšãããšãã«ãRegexOptions.Singleline èšèªèŠçŽ ã®åäœãå€åããäŸã次ã«ç€ºããŸãã æ£èŠè¡šçŸ ^.+ ã¯æååã®å
é ããéå§ãããã¹ãŠã®æåãšäžèŽããŸãã æ¢å®ã§ã¯ãç
§å㯠1 è¡ç®ã®æ«å°Ÿã§çµäºããŸããæ£èŠè¡šçŸãã¿ãŒã³ã¯åŸ©åž°æå \r ãšäžèŽããŸããã\n ãšã¯äžèŽããŸããã
RegexOptions.Singleline ãªãã·ã§ã³ã¯å
¥åæååå
šäœãåäžè¡ãšããŠè§£éããã®ã§ã\n ãå«ãå
¥åæååå
ã®ãã¹ãŠã®æåãšäžèŽããŸãã
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = "^.+";
string input = "This is one line and" + Environment.NewLine + "this is the second.";
foreach (Match match in Regex.Matches(input, pattern))
Console.WriteLine(Regex.Escape(match.Value));
Console.WriteLine();
foreach (Match match in Regex.Matches(input, pattern, RegexOptions.Singleline))
Console.WriteLine(Regex.Escape(match.Value));
}
}
// The example displays the following output:
// This\ is\ one\ line\ and\r
//
// This\ is\ one\ line\ and\r\nthis\ is\ the\ second\.
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim pattern As String = "^.+"
Dim input As String = "This is one line and" + vbCrLf + "this is the second."
For Each match As Match In Regex.Matches(input, pattern)
Console.WriteLine(Regex.Escape(match.Value))
Next
Console.WriteLine()
For Each match As Match In Regex.Matches(input, pattern, RegexOptions.SingleLine)
Console.WriteLine(Regex.Escape(match.Value))
Next
End Sub
End Module
' The example displays the following output:
' This\ is\ one\ line\ and\r
'
' This\ is\ one\ line\ and\r\nthis\ is\ the\ second\.
次ã®äŸã¯ãåã®äŸãšç䟡ã§ãããã€ã³ã©ã€ã³ ãªãã·ã§ã³ (?s) ã䜿çšããŠåäžè¡ã¢ãŒããæå¹ã«ããŠããç¹ãç°ãªããŸãã
using System;
using System.Text.RegularExpressions;
public class SingleLineExample
{
public static void Main()
{
string pattern = "(?s)^.+";
string input = "This is one line and" + Environment.NewLine + "this is the second.";
foreach (Match match in Regex.Matches(input, pattern))
Console.WriteLine(Regex.Escape(match.Value));
}
}
// The example displays the following output:
// This\ is\ one\ line\ and\r\nthis\ is\ the\ second\.
Imports System.Text.RegularExpressions
Module SingleLineExample
Public Sub Main()
Dim pattern As String = "(?s)^.+"
Dim input As String = "This is one line and" + vbCrLf + "this is the second."
For Each match As Match In Regex.Matches(input, pattern)
Console.WriteLine(Regex.Escape(match.Value))
Next
End Sub
End Module
' The example displays the following output:
' This\ is\ one\ line\ and\r\nthis\ is\ the\ second\.
æç€ºçãªãã£ããã£ã®ã¿
æ¢å®ã§ã¯ãæ£èŠè¡šçŸãã¿ãŒã³ã«ãããŠãã£ããã£ã°ã«ãŒãã¯æ¬åŒ§ã®äœ¿çšã«ãã£ãŠå®çŸ©ãããŸãã ååä»ãã®ã°ã«ãŒãã«ã¯ (?<name>subexpression) èšèªãªãã·ã§ã³ã§ååãŸãã¯çªå·ãäžããããã®ã«å¯ŸããŠãååã®ãªãã°ã«ãŒãã«ã¯ã€ã³ããã¯ã¹ã§ã¢ã¯ã»ã¹ã§ããŸãã
GroupCollection ãªããžã§ã¯ãã§ã¯ãååã®ãªãã°ã«ãŒããååä»ãã®ã°ã«ãŒããããåªå
ãããŸãã
ã°ã«ãŒãåæ§æäœã¯éåžžãè€æ°ã®èšèªèŠçŽ ã«éæå®åãé©çšããå Žåã«ã®ã¿äœ¿çšããããã£ããã£ãããéšåæååã¯å¯Ÿè±¡ã«ãªããŸããã ããšãã°ãæ¬¡ã®æ£èŠè¡šçŸã«ã€ããŠèããŸãã
\b\(?((\w+),?\s?)+[\.!?]\)?
ãã®æ£èŠè¡šçŸã¯ãããã¥ã¡ã³ãããææ«ãããªãªããæå笊ããŸãã¯çå笊ã§ããæãæœåºããããšã®ã¿ãç®çãšããçµæã®æ (Match ãªããžã§ã¯ãã§è¡šããã) ã®ã¿ã察象ãšããŠããŸãã ã³ã¬ã¯ã·ã§ã³å ã®åã ã®åèªã¯å¯Ÿè±¡ã§ã¯ãããŸããã
æ£èŠè¡šçŸãšã³ãžã³ã§ GroupCollection ã³ã¬ã¯ã·ã§ã³ ãªããžã§ã¯ããš CaptureCollection ã³ã¬ã¯ã·ã§ã³ ãªããžã§ã¯ãã®äž¡æ¹ãèšå®ããå¿
èŠãããã®ã§ããã£ãã㣠ã°ã«ãŒãã以åŸã䜿çšãããªãå Žåã¯ããã®èšå®ã®åŠçãç¡é§ã«ãªãå¯èœæ§ããããŸãã å¥ã®æ¹æ³ãšããŠã¯ãRegexOptions.ExplicitCapture ãªãã·ã§ã³ãŸã㯠n ã€ã³ã©ã€ã³ ãªãã·ã§ã³ã䜿çšããŠã(?<name>subexpression) æ§æäœã«ãã£ãŠæå®ãããæç€ºçãªååãŸãã¯çªå·ä»ãã®ã°ã«ãŒãã®ã¿ãæå¹ãªãã£ããã£å¯Ÿè±¡ãšããŠæå®ããæ¹æ³ãæããããŸãã
次ã®äŸã¯ã\b\(?((\w+),?\s?)+[\.!?]\)? æ£èŠè¡šçŸãã¿ãŒã³ã«ãã£ãŠè¿ãããäžèŽã«é¢ããæ
å ±ã瀺ããŠããŸã (Match ã¡ãœããã RegexOptions.ExplicitCapture ãªãã·ã§ã³ã䜿çšããŠåŒã³åºãããå Žåãããã³äœ¿çšããã«åŒã³åºãããå Žå)ã æåã®ã¡ãœããåŒã³åºãã®åºåçµæã瀺ãããã«ãæ£èŠè¡šçŸãšã³ãžã³ã§ã¯ããã£ããã£ããéšåæååã«é¢ããæ
å ±ã«åºã¥ããŠãGroupCollection ã³ã¬ã¯ã·ã§ã³ ãªããžã§ã¯ãããã³ CaptureCollection ã³ã¬ã¯ã·ã§ã³ ãªããžã§ã¯ããå®å
šã«èšå®ãããŠããŸãã 2 çªç®ã®ã¡ãœããã¯ãoptions ã RegexOptions.ExplicitCapture ã«èšå®ããŠåŒã³åºãããŠããã®ã§ãã°ã«ãŒãã«é¢ããæ
å ±ããã£ããã£ããŸããã
using System;
using System.Text.RegularExpressions;
public class Explicit1Example
{
public static void Main()
{
string input = "This is the first sentence. Is it the beginning " +
"of a literary masterpiece? I think not. Instead, " +
"it is a nonsensical paragraph.";
string pattern = @"\b\(?((?>\w+),?\s?)+[\.!?]\)?";
Console.WriteLine("With implicit captures:");
foreach (Match match in Regex.Matches(input, pattern))
{
Console.WriteLine($"The match: {match.Value}");
int groupCtr = 0;
foreach (Group group in match.Groups)
{
Console.WriteLine($" Group {groupCtr}: {group.Value}");
groupCtr++;
int captureCtr = 0;
foreach (Capture capture in group.Captures)
{
Console.WriteLine($" Capture {captureCtr}: {capture.Value}");
captureCtr++;
}
}
}
Console.WriteLine();
Console.WriteLine("With explicit captures only:");
foreach (Match match in Regex.Matches(input, pattern, RegexOptions.ExplicitCapture))
{
Console.WriteLine($"The match: {match.Value}");
int groupCtr = 0;
foreach (Group group in match.Groups)
{
Console.WriteLine($" Group {groupCtr}: {group.Value}");
groupCtr++;
int captureCtr = 0;
foreach (Capture capture in group.Captures)
{
Console.WriteLine($" Capture {captureCtr}: {capture.Value}");
captureCtr++;
}
}
}
}
}
// The example displays the following output:
// With implicit captures:
// The match: This is the first sentence.
// Group 0: This is the first sentence.
// Capture 0: This is the first sentence.
// Group 1: sentence
// Capture 0: This
// Capture 1: is
// Capture 2: the
// Capture 3: first
// Capture 4: sentence
// Group 2: sentence
// Capture 0: This
// Capture 1: is
// Capture 2: the
// Capture 3: first
// Capture 4: sentence
// The match: Is it the beginning of a literary masterpiece?
// Group 0: Is it the beginning of a literary masterpiece?
// Capture 0: Is it the beginning of a literary masterpiece?
// Group 1: masterpiece
// Capture 0: Is
// Capture 1: it
// Capture 2: the
// Capture 3: beginning
// Capture 4: of
// Capture 5: a
// Capture 6: literary
// Capture 7: masterpiece
// Group 2: masterpiece
// Capture 0: Is
// Capture 1: it
// Capture 2: the
// Capture 3: beginning
// Capture 4: of
// Capture 5: a
// Capture 6: literary
// Capture 7: masterpiece
// The match: I think not.
// Group 0: I think not.
// Capture 0: I think not.
// Group 1: not
// Capture 0: I
// Capture 1: think
// Capture 2: not
// Group 2: not
// Capture 0: I
// Capture 1: think
// Capture 2: not
// The match: Instead, it is a nonsensical paragraph.
// Group 0: Instead, it is a nonsensical paragraph.
// Capture 0: Instead, it is a nonsensical paragraph.
// Group 1: paragraph
// Capture 0: Instead,
// Capture 1: it
// Capture 2: is
// Capture 3: a
// Capture 4: nonsensical
// Capture 5: paragraph
// Group 2: paragraph
// Capture 0: Instead
// Capture 1: it
// Capture 2: is
// Capture 3: a
// Capture 4: nonsensical
// Capture 5: paragraph
//
// With explicit captures only:
// The match: This is the first sentence.
// Group 0: This is the first sentence.
// Capture 0: This is the first sentence.
// The match: Is it the beginning of a literary masterpiece?
// Group 0: Is it the beginning of a literary masterpiece?
// Capture 0: Is it the beginning of a literary masterpiece?
// The match: I think not.
// Group 0: I think not.
// Capture 0: I think not.
// The match: Instead, it is a nonsensical paragraph.
// Group 0: Instead, it is a nonsensical paragraph.
// Capture 0: Instead, it is a nonsensical paragraph.
Imports System.Text.RegularExpressions
Module Explicit1Example
Public Sub Main()
Dim input As String = "This is the first sentence. Is it the beginning " +
"of a literary masterpiece? I think not. Instead, " +
"it is a nonsensical paragraph."
Dim pattern As String = "\b\(?((?>\w+),?\s?)+[\.!?]\)?"
Console.WriteLine("With implicit captures:")
For Each match As Match In Regex.Matches(input, pattern)
Console.WriteLine("The match: {0}", match.Value)
Dim groupCtr As Integer = 0
For Each group As Group In match.Groups
Console.WriteLine(" Group {0}: {1}", groupCtr, group.Value)
groupCtr += 1
Dim captureCtr As Integer = 0
For Each capture As Capture In group.Captures
Console.WriteLine(" Capture {0}: {1}", captureCtr, capture.Value)
captureCtr += 1
Next
Next
Next
Console.WriteLine()
Console.WriteLine("With explicit captures only:")
For Each match As Match In Regex.Matches(input, pattern, RegexOptions.ExplicitCapture)
Console.WriteLine("The match: {0}", match.Value)
Dim groupCtr As Integer = 0
For Each group As Group In match.Groups
Console.WriteLine(" Group {0}: {1}", groupCtr, group.Value)
groupCtr += 1
Dim captureCtr As Integer = 0
For Each capture As Capture In group.Captures
Console.WriteLine(" Capture {0}: {1}", captureCtr, capture.Value)
captureCtr += 1
Next
Next
Next
End Sub
End Module
' The example displays the following output:
' With implicit captures:
' The match: This is the first sentence.
' Group 0: This is the first sentence.
' Capture 0: This is the first sentence.
' Group 1: sentence
' Capture 0: This
' Capture 1: is
' Capture 2: the
' Capture 3: first
' Capture 4: sentence
' Group 2: sentence
' Capture 0: This
' Capture 1: is
' Capture 2: the
' Capture 3: first
' Capture 4: sentence
' The match: Is it the beginning of a literary masterpiece?
' Group 0: Is it the beginning of a literary masterpiece?
' Capture 0: Is it the beginning of a literary masterpiece?
' Group 1: masterpiece
' Capture 0: Is
' Capture 1: it
' Capture 2: the
' Capture 3: beginning
' Capture 4: of
' Capture 5: a
' Capture 6: literary
' Capture 7: masterpiece
' Group 2: masterpiece
' Capture 0: Is
' Capture 1: it
' Capture 2: the
' Capture 3: beginning
' Capture 4: of
' Capture 5: a
' Capture 6: literary
' Capture 7: masterpiece
' The match: I think not.
' Group 0: I think not.
' Capture 0: I think not.
' Group 1: not
' Capture 0: I
' Capture 1: think
' Capture 2: not
' Group 2: not
' Capture 0: I
' Capture 1: think
' Capture 2: not
' The match: Instead, it is a nonsensical paragraph.
' Group 0: Instead, it is a nonsensical paragraph.
' Capture 0: Instead, it is a nonsensical paragraph.
' Group 1: paragraph
' Capture 0: Instead,
' Capture 1: it
' Capture 2: is
' Capture 3: a
' Capture 4: nonsensical
' Capture 5: paragraph
' Group 2: paragraph
' Capture 0: Instead
' Capture 1: it
' Capture 2: is
' Capture 3: a
' Capture 4: nonsensical
' Capture 5: paragraph
'
' With explicit captures only:
' The match: This is the first sentence.
' Group 0: This is the first sentence.
' Capture 0: This is the first sentence.
' The match: Is it the beginning of a literary masterpiece?
' Group 0: Is it the beginning of a literary masterpiece?
' Capture 0: Is it the beginning of a literary masterpiece?
' The match: I think not.
' Group 0: I think not.
' Capture 0: I think not.
' The match: Instead, it is a nonsensical paragraph.
' Group 0: Instead, it is a nonsensical paragraph.
' Capture 0: Instead, it is a nonsensical paragraph.
æ£èŠè¡šçŸãã¿ãŒã³ \b\(?((?>\w+),?\s?)+[\.!?]\)? ã¯ã次ã®è¡šã«ç€ºãããã«å®çŸ©ãããŠããŸãã
| ãã¿ãŒã³ | 説æ |
|---|---|
\b |
ã¯ãŒãå¢çã§å§ããŠãã ããã |
\(? |
éãæ¬åŒ§ ("(") ã®åºçŸã0åãŸãã¯1åãšäžèŽããŸãã |
(?>\w+),? |
1 å以äžã®åèªæåã®åŸã« 0 åãŸã㯠1 åã®ã³ã³ããç¶ããã¿ãŒã³ãšäžèŽããŸãã åèªæåãç §åãããšãã«ããã¯ãã©ãã¯ããªãã§ãã ããã |
\s? |
0 åãŸã㯠1 åã®ç©ºçœæåãšäžèŽããŸãã |
((\w+),?\s?)+ |
1 å以äžã®åèªæåã0 åãŸã㯠1 åã®ã³ã³ããããã³ 0 åãŸã㯠1 åã®ç©ºçœæåã 1 å以äžç¶ããã¿ãŒã³ãšäžèŽããŸãã |
[\.!?]\)? |
3 çš®é¡ã®åºåãèšå·ã®ããããã®åŸã« 0 åãŸã㯠1 åã®å³ãã£ã (")") ãç¶ããã¿ãŒã³ãšäžèŽããŸãã |
(?n) ã€ã³ã©ã€ã³èŠçŽ ã䜿çšããŠãèªåçãªãã£ããã£ãæå¶ããããšãã§ããŸãã æ¬¡ã®äŸã§ã¯ãåã®äŸã®æ£èŠè¡šçŸãã¿ãŒã³ã倿ŽããŠã(?n) ã€ã³ã©ã€ã³èŠçŽ ã RegexOptions.ExplicitCapture ãªãã·ã§ã³ã®ä»£ããã«äœ¿çšããŠããŸãã
using System;
using System.Text.RegularExpressions;
public class Explicit2Example
{
public static void Main()
{
string input = "This is the first sentence. Is it the beginning " +
"of a literary masterpiece? I think not. Instead, " +
"it is a nonsensical paragraph.";
string pattern = @"(?n)\b\(?((?>\w+),?\s?)+[\.!?]\)?";
foreach (Match match in Regex.Matches(input, pattern))
{
Console.WriteLine($"The match: {match.Value}");
int groupCtr = 0;
foreach (Group group in match.Groups)
{
Console.WriteLine($" Group {groupCtr}: {group.Value}");
groupCtr++;
int captureCtr = 0;
foreach (Capture capture in group.Captures)
{
Console.WriteLine($" Capture {captureCtr}: {capture.Value}");
captureCtr++;
}
}
}
}
}
// The example displays the following output:
// The match: This is the first sentence.
// Group 0: This is the first sentence.
// Capture 0: This is the first sentence.
// The match: Is it the beginning of a literary masterpiece?
// Group 0: Is it the beginning of a literary masterpiece?
// Capture 0: Is it the beginning of a literary masterpiece?
// The match: I think not.
// Group 0: I think not.
// Capture 0: I think not.
// The match: Instead, it is a nonsensical paragraph.
// Group 0: Instead, it is a nonsensical paragraph.
// Capture 0: Instead, it is a nonsensical paragraph.
Imports System.Text.RegularExpressions
Module Explicit2Example
Public Sub Main()
Dim input As String = "This is the first sentence. Is it the beginning " +
"of a literary masterpiece? I think not. Instead, " +
"it is a nonsensical paragraph."
Dim pattern As String = "(?n)\b\(?((?>\w+),?\s?)+[\.!?]\)?"
For Each match As Match In Regex.Matches(input, pattern)
Console.WriteLine("The match: {0}", match.Value)
Dim groupCtr As Integer = 0
For Each group As Group In match.Groups
Console.WriteLine(" Group {0}: {1}", groupCtr, group.Value)
groupCtr += 1
Dim captureCtr As Integer = 0
For Each capture As Capture In group.Captures
Console.WriteLine(" Capture {0}: {1}", captureCtr, capture.Value)
captureCtr += 1
Next
Next
Next
End Sub
End Module
' The example displays the following output:
' The match: This is the first sentence.
' Group 0: This is the first sentence.
' Capture 0: This is the first sentence.
' The match: Is it the beginning of a literary masterpiece?
' Group 0: Is it the beginning of a literary masterpiece?
' Capture 0: Is it the beginning of a literary masterpiece?
' The match: I think not.
' Group 0: I think not.
' Capture 0: I think not.
' The match: Instead, it is a nonsensical paragraph.
' Group 0: Instead, it is a nonsensical paragraph.
' Capture 0: Instead, it is a nonsensical paragraph.
æåŸã«ã(?n:) ã€ã³ã©ã€ã³ ã°ã«ãŒãèŠçŽ ã䜿çšããŠãã°ã«ãŒãããšã«èªåçãªãã£ããã£ãæå¶ããããšãã§ããŸãã æ¬¡ã®äŸã§ã¯ãåã®ãã¿ãŒã³ã倿ŽããŠãå€éšã°ã«ãŒã ((?>\w+),?\s?) ã§ååã®ãªããã£ããã£ãæå¶ããŠããŸãã ãã®åŠçã§ã¯ãå
éšã°ã«ãŒãã§ã®ååã®ãªããã£ããã£ãæå¶ãããããšã«æ³šæããŠãã ããã
using System;
using System.Text.RegularExpressions;
public class Explicit3Example
{
public static void Main()
{
string input = "This is the first sentence. Is it the beginning " +
"of a literary masterpiece? I think not. Instead, " +
"it is a nonsensical paragraph.";
string pattern = @"\b\(?(?n:(?>\w+),?\s?)+[\.!?]\)?";
foreach (Match match in Regex.Matches(input, pattern))
{
Console.WriteLine($"The match: {match.Value}");
int groupCtr = 0;
foreach (Group group in match.Groups)
{
Console.WriteLine($" Group {groupCtr}: {group.Value}");
groupCtr++;
int captureCtr = 0;
foreach (Capture capture in group.Captures)
{
Console.WriteLine($" Capture {captureCtr}: {capture.Value}");
captureCtr++;
}
}
}
}
}
// The example displays the following output:
// The match: This is the first sentence.
// Group 0: This is the first sentence.
// Capture 0: This is the first sentence.
// The match: Is it the beginning of a literary masterpiece?
// Group 0: Is it the beginning of a literary masterpiece?
// Capture 0: Is it the beginning of a literary masterpiece?
// The match: I think not.
// Group 0: I think not.
// Capture 0: I think not.
// The match: Instead, it is a nonsensical paragraph.
// Group 0: Instead, it is a nonsensical paragraph.
// Capture 0: Instead, it is a nonsensical paragraph.
Imports System.Text.RegularExpressions
Module Explicit3Example
Public Sub Main()
Dim input As String = "This is the first sentence. Is it the beginning " +
"of a literary masterpiece? I think not. Instead, " +
"it is a nonsensical paragraph."
Dim pattern As String = "\b\(?(?n:(?>\w+),?\s?)+[\.!?]\)?"
For Each match As Match In Regex.Matches(input, pattern)
Console.WriteLine("The match: {0}", match.Value)
Dim groupCtr As Integer = 0
For Each group As Group In match.Groups
Console.WriteLine(" Group {0}: {1}", groupCtr, group.Value)
groupCtr += 1
Dim captureCtr As Integer = 0
For Each capture As Capture In group.Captures
Console.WriteLine(" Capture {0}: {1}", captureCtr, capture.Value)
captureCtr += 1
Next
Next
Next
End Sub
End Module
' The example displays the following output:
' The match: This is the first sentence.
' Group 0: This is the first sentence.
' Capture 0: This is the first sentence.
' The match: Is it the beginning of a literary masterpiece?
' Group 0: Is it the beginning of a literary masterpiece?
' Capture 0: Is it the beginning of a literary masterpiece?
' The match: I think not.
' Group 0: I think not.
' Capture 0: I think not.
' The match: Instead, it is a nonsensical paragraph.
' Group 0: Instead, it is a nonsensical paragraph.
' Capture 0: Instead, it is a nonsensical paragraph.
ã³ã³ãã€ã«ãããæ£èŠè¡šçŸ
泚æ
å¯èœãªéãã¯ã ãªãã·ã§ã³ã䜿çšããŠæ£èŠè¡šçŸãã³ã³ãã€ã«ããã®ã§ãªãããœãŒã¹çæãããæ£èŠè¡šçŸRegexOptions.Compiledã䜿çšããŠãã ããã ãœãŒã¹çæãè¡ããšãã¢ããªã®èµ·åããã³å®è¡ãããè¿ éã«ãªããããªãã³ã°ããããããªããŸãã ãœãŒã¹çæãå¯èœãªå Žåã«ã€ããŠã¯ãããã€äœ¿çšãããããåç §ããŠãã ããã
æ¢å®ã§ã¯ã.NETã®æ£èŠè¡šçŸãè§£éãããŸãã Regex ãªããžã§ã¯ããã€ã³ã¹ã¿ã³ã¹åãããããéç Regex ã¡ãœãããåŒã³åºããããšãã«ãæ£èŠè¡šçŸãã¿ãŒã³ã¯ã«ã¹ã¿ã ãªãã³ãŒãã®ã»ããã«è§£æãããã€ã³ã¿ãŒããªã¿ãŒããã®ãªãã³ãŒãã«åºã¥ããŠæ£èŠè¡šçŸãå®è¡ããŸãã ããã«ã¯ãã¬ãŒããªãã䌎ããŸããæ£èŠè¡šçŸãšã³ãžã³ãåæåããã³ã¹ãã¯ãå®è¡æã®ããã©ãŒãã³ã¹ãç ç²ã«ããŠæå°éã«æããããŸãã
æ£èŠè¡šçŸã鿬¡è§£éãã代ããã«ãRegexOptions.Compiled ãªãã·ã§ã³ã䜿çšããŠã³ã³ãã€ã«ãããæ£èŠè¡šçŸã䜿çšã§ããŸãã ãã®å Žåããã¿ãŒã³ã¯æ£èŠè¡šçŸãšã³ãžã³ã«æž¡ããããªãã³ãŒãã®ã»ããã«è§£æãããŠãããå ±éäžéèšèª (CIL) ã«å€æãããŸãã倿ãããã³ãŒãã¯ãå ±éèšèªã©ã³ã¿ã€ã ã«çŽæ¥æž¡ãããšãã§ããŸãã ã³ã³ãã€ã«ãããæ£èŠè¡šçŸã¯ãåæåæéãç ç²ã«ããŠå®è¡æã®ããã©ãŒãã³ã¹ãæå€§åããŸãã
泚æ
æ£èŠè¡šçŸãã³ã³ãã€ã«ããã«ã¯ãRegexOptions.Compiled å€ã options ã¯ã©ã¹ ã³ã³ã¹ãã©ã¯ã¿ãŒãŸãã¯éçãã¿ãŒã³äžèŽã¡ãœããã® Regex ãã©ã¡ãŒã¿ãŒã«æž¡ãå¿
èŠããããŸãã ããã¯ã€ã³ã©ã€ã³ ãªãã·ã§ã³ãšããŠã¯å©çšã§ããŸããã
ã³ã³ãã€ã«ãããæ£èŠè¡šçŸã¯ãéçæ£èŠè¡šçŸãšã€ã³ã¹ã¿ã³ã¹æ£èŠè¡šçŸã®äž¡æ¹ã®åŒã³åºãã«äœ¿çšã§ããŸãã éçæ£èŠè¡šçŸã§ã¯ãRegexOptions.Compiled ãªãã·ã§ã³ã¯æ£èŠè¡šçŸãã¿ãŒã³äžèŽã¡ãœããã® options ãã©ã¡ãŒã¿ãŒã«æž¡ãããŸãã ã€ã³ã¹ã¿ã³ã¹æ£èŠè¡šçŸã§ã¯ããã㯠options ã¯ã©ã¹ ã³ã³ã¹ãã©ã¯ã¿ãŒã® Regex ãã©ã¡ãŒã¿ãŒã«æž¡ãããŸãã ã©ã¡ãã®å Žåããããã©ãŒãã³ã¹ãåäžããŸãã
ãã ããããã©ãŒãã³ã¹ãåäžããã®ã¯ãæ¬¡ã®æ¡ä»¶ãæºãããŠããå Žåã«éå®ãããŸãã
ç¹å®ã®æ£èŠè¡šçŸã衚ã Regex ãªããžã§ã¯ããæ£èŠè¡šçŸãã¿ãŒã³äžèŽã¡ãœããã®è€æ°ã®åŒã³åºãã§äœ¿çšãããŠããã
Regex ãªããžã§ã¯ããã¹ã³ãŒãã®å€ã«åºãããšãèš±å¯ãããŠããããåå©çšã§ããã
éçæ£èŠè¡šçŸãæ£èŠè¡šçŸãã¿ãŒã³äžèŽã¡ãœããã®è€æ°ã®åŒã³åºãã§äœ¿çšãããŠãã (éçã¡ãœããåŒã³åºãã§äœ¿çšããæ£èŠè¡šçŸã¯æ£èŠè¡šçŸãšã³ãžã³ã«ãã£ãŠãã£ãã·ã¥ãããã®ã§ãããã©ãŒãã³ã¹ã®åäžãå¯èœã«ãªããŸã)ã
泚æ
RegexOptions.Compiled ãªãã·ã§ã³ã¯ãäºåå®çŸ©ãããã³ã³ãã€ã«æžã¿ã®æ£èŠè¡šçŸãå«ãç¹æ®ãªç®çã®ã¢ã»ã³ããªãäœæãã Regex.CompileToAssembly ã¡ãœãããšã¯é¢ä¿ãããŸããã
空çœãç¡èŠ
æ¢å®ã§ã¯ãæ£èŠè¡šçŸãã¿ãŒã³ã«å«ãŸãã空çœã«ã¯éèŠãªæå³ããããæ£èŠè¡šçŸãšã³ãžã³ã§ã¯ãå
¥åæååå
ã®ç©ºçœæåãšã®ç
§åã匷å¶ãããŸãã ãã®çµæãæ£èŠè¡šçŸ "\b\w+\s" ããã³ "\b\w+" ã¯ãã»ãŒçäŸ¡ãªæ£èŠè¡šçŸã§ãããšèšããŸãã ããã«ãã·ã£ãŒãèšå· (#) ãæ£èŠè¡šçŸãã¿ãŒã³ã«å«ãŸããŠããå Žåã¯ãç
§åãã察象ã®ãªãã©ã«æåãšããŠè§£éãããŸãã
RegexOptions.IgnorePatternWhitespace ãªãã·ã§ã³ãŸã㯠x ã€ã³ã©ã€ã³ ãªãã·ã§ã³ãæå®ãããšããã®æ¢å®ã®åäœã¯æ¬¡ã®ããã«å€æŽãããŸãã
æ£èŠè¡šçŸãã¿ãŒã³ã§ãšã¹ã±ãŒããããŠããªã空çœã¯ç¡èŠãããŸãã æ£èŠè¡šçŸãã¿ãŒã³ã«ç©ºçœæåãå«ããå Žåããããã¯ãšã¹ã±ãŒãããå¿ èŠããããŸãïŒäŸãã°ã
\sã "\" ãªã©ïŒãããã·ã¥èšå· (#) ã¯ããªãã©ã«æåã§ã¯ãªããã³ã¡ã³ãã®å é ãšããŠè§£éãããŸãã æ£èŠè¡šçŸãã¿ãŒã³ã«å«ãŸãã
#æåããæ¬¡ã®\næåããŸãã¯æååã®æåŸãŸã§ã®ãã¹ãŠã®ããã¹ãã¯ãã³ã¡ã³ããšããŠè§£éãããŸãã
ãã ããæ¬¡ã«èª¬æããç¶æ³ã§ã¯ãRegexOptions.IgnorePatternWhitespace ãªãã·ã§ã³ã䜿çšããŠããå Žåã§ããæ£èŠè¡šçŸã®ç©ºçœæåã¯ç¡èŠãããŸããã
æåã¯ã©ã¹å ã®ç©ºçœã¯åžžã«æåéãè§£éãããŸãã ããšãã°ãæ£èŠè¡šçŸãã¿ãŒã³
[ .,;:]ã¯ãç©ºçœæåãããªãªããã³ã³ããã»ãã³ãã³ããŸãã¯ã³ãã³ã®ä»»æã® 1 æåãšäžèŽããŸãããã£ãã§å²ãŸããéæå®å (
{n}ã{n,}ã{n,m}ãªã©) ã«ã¯ç©ºçœã䜿çšã§ããŸããã ããšãã°ãæ£èŠè¡šçŸãã¿ãŒã³\d{1, 3}ã¯ãç©ºçœæåãå«ãŸããŠããããã1 ïœ 3 æ¡ã®æ°å€ã®ã©ã®ã·ãŒã±ã³ã¹ã«ãäžèŽããŸãããèšèªèŠçŽ ãå°å ¥ããæåã·ãŒã±ã³ã¹å ã§ã¯ç©ºçœã䜿çšã§ããŸããã æ¬¡ã«äŸã瀺ããŸãã
èšèªèŠçŽ
(?:subexpression)ã¯éãã£ãã㣠ã°ã«ãŒãã衚ããèŠçŽ ã®(?:éšåã«ã¯ç©ºçœãåã蟌ãããšã¯ã§ããŸããã ãã¿ãŒã³(? :éšååŒ)ã¯ãæ£èŠè¡šçŸãšã³ãžã³ããã¿ãŒã³ãè§£æã§ããªããããå®è¡æã«ArgumentExceptionãã¹ããŒããŸãããããŠããã¿ãŒã³( ?:éšååŒ)ã¯éšååŒãšäžèŽããŸãããUnicode ã«ããŽãªãŸãã¯ååä»ããããã¯ã衚ãèšèªèŠçŽ
\p{name}ã®\p{éšåã«ã¯ã空çœãåã蟌ãããšã¯ã§ããŸããã 空çœã远å ãããšããã®èŠçŽ ã¯å®è¡æã« ArgumentException ãã¹ããŒããŸãã
ãã®ãªãã·ã§ã³ãæå¹ã«ãããšãå€ãã®å Žåãè§£æãçè§£ãå°é£ãªæ£èŠè¡šçŸãç°¡ç¥åã§ããŸãã æ£èŠè¡šçŸãèªã¿ããããªãã説æãèšè¿°ã§ããŸãã
次ã®äŸã§ã¯ãæ£èŠè¡šçŸãã¿ãŒã³ãå®çŸ©ããŠããŸãã
\b \(? ( (?>\w+) ,?\s? )+ [\.!?] \)? # Matches an entire sentence.
ãã®ãã¿ãŒã³ã¯ããæç€ºçãªãã£ããã£ã®ã¿ãã»ã¯ã·ã§ã³ã§å®çŸ©ãããã¿ãŒã³ãšäŒŒãŠããŸãããã ããRegexOptions.IgnorePatternWhitespace ãªãã·ã§ã³ã䜿çšããŠããã¿ãŒã³ã®ç©ºçœãç¡èŠããŠããç¹ãç°ãªããŸãã
using System;
using System.Text.RegularExpressions;
public class Whitespace1Example
{
public static void Main()
{
string input = "This is the first sentence. Is it the beginning " +
"of a literary masterpiece? I think not. Instead, " +
"it is a nonsensical paragraph.";
string pattern = @"\b \(? ( (?>\w+) ,?\s? )+ [\.!?] \)? # Matches an entire sentence.";
foreach (Match match in Regex.Matches(input, pattern, RegexOptions.IgnorePatternWhitespace))
Console.WriteLine(match.Value);
}
}
// The example displays the following output:
// This is the first sentence.
// Is it the beginning of a literary masterpiece?
// I think not.
// Instead, it is a nonsensical paragraph.
Imports System.Text.RegularExpressions
Module Whitespace1Example
Public Sub Main()
Dim input As String = "This is the first sentence. Is it the beginning " +
"of a literary masterpiece? I think not. Instead, " +
"it is a nonsensical paragraph."
Dim pattern As String = "\b \(? ( (?>\w+) ,?\s? )+ [\.!?] \)? # Matches an entire sentence."
For Each match As Match In Regex.Matches(input, pattern, RegexOptions.IgnorePatternWhitespace)
Console.WriteLine(match.Value)
Next
End Sub
End Module
' The example displays the following output:
' This is the first sentence.
' Is it the beginning of a literary masterpiece?
' I think not.
' Instead, it is a nonsensical paragraph.
次ã®äŸã§ã¯ã(?x) ã€ã³ã©ã€ã³ ãªãã·ã§ã³ã䜿çšããŠããã¿ãŒã³ã®ç©ºçœãç¡èŠããŠããŸãã
using System;
using System.Text.RegularExpressions;
public class Whitespace2Example
{
public static void Main()
{
string input = "This is the first sentence. Is it the beginning " +
"of a literary masterpiece? I think not. Instead, " +
"it is a nonsensical paragraph.";
string pattern = @"(?x)\b \(? ( (?>\w+) ,?\s? )+ [\.!?] \)? # Matches an entire sentence.";
foreach (Match match in Regex.Matches(input, pattern))
Console.WriteLine(match.Value);
}
}
// The example displays the following output:
// This is the first sentence.
// Is it the beginning of a literary masterpiece?
// I think not.
// Instead, it is a nonsensical paragraph.
Imports System.Text.RegularExpressions
Module Whitespace2Example
Public Sub Main()
Dim input As String = "This is the first sentence. Is it the beginning " +
"of a literary masterpiece? I think not. Instead, " +
"it is a nonsensical paragraph."
Dim pattern As String = "(?x)\b \(? ( (?>\w+) ,?\s? )+ [\.!?] \)? # Matches an entire sentence."
For Each match As Match In Regex.Matches(input, pattern)
Console.WriteLine(match.Value)
Next
End Sub
End Module
' The example displays the following output:
' This is the first sentence.
' Is it the beginning of a literary masterpiece?
' I think not.
' Instead, it is a nonsensical paragraph.
å³ããå·Šã¢ãŒã
æ¢å®ã§ã¯ãæ£èŠè¡šçŸãšã³ãžã³ã¯å·Šããå³ã®æ¹åã«æ€çŽ¢ããŸãã ãã®æ€çŽ¢ã®æ¹åãå転ããã«ã¯ãRegexOptions.RightToLeft ãªãã·ã§ã³ã䜿çšããŸãã å³ããå·Šã®æ€çŽ¢ã¯ãæååã®æåŸã®æåäœçœ®ããèªåçã«éå§ãããŸãã Regex.Match(String, Int32) ãªã©ãéå§äœçœ®ã®ãã©ã¡ãŒã¿ãŒãå«ããã¿ãŒã³äžèŽã¡ãœããã®å Žåãæå®ã®éå§äœçœ®ã¯å³ç«¯ã®æåäœçœ®ã®ã€ã³ããã¯ã¹ã§ãããããããæ€çŽ¢ãéå§ããŸãã
泚æ
å³ããå·Šã¢ãŒãã䜿çšããã«ã¯ãRegexOptions.RightToLeft å€ã options ã¯ã©ã¹ ã³ã³ã¹ãã©ã¯ã¿ãŒãŸãã¯éçãã¿ãŒã³äžèŽã¡ãœããã® Regex ãã©ã¡ãŒã¿ãŒã«æž¡ãå¿
èŠããããŸãã ã€ã³ã©ã€ã³ ãªãã·ã§ã³ãšããŠã¯äœ¿çšã§ããŸããã
äŸ
æ£èŠè¡šçŸ \bb\w+\s ã¯ãæå "b" ããå§ãŸãè€æ°æåã®åèªãšãã®åŸã®ç©ºçœæåãšäžèŽããŸãã æ¬¡ã®äŸã§ã¯ãå
¥åæååã¯ã1 æå以äžã® "b" ãå«ã 3 ã€ã®åèªã§æ§æãããŠããŸãã 1 çªç®ãš 2 çªç®ã®åèªã¯ "b" ã§å§ãŸãã3 çªç®ã®åèªã¯ "b" ã§çµãããŸãã å³ããå·Šã®æ€çŽ¢ã®äŸã瀺ãããã«ã1 çªç®ãš 2 çªç®ã®åèªã®ã¿ãæ£èŠè¡šçŸãã¿ãŒã³ãšäžèŽãã2 çªç®ã®åèªãæåã«äžèŽããŸãã
using System;
using System.Text.RegularExpressions;
public class RTL1Example
{
public static void Main()
{
string pattern = @"\bb\w+\s";
string input = "build band tab";
foreach (Match match in Regex.Matches(input, pattern, RegexOptions.RightToLeft))
Console.WriteLine($"'{match.Value}' found at position {match.Index}.");
}
}
// The example displays the following output:
// 'band ' found at position 6.
// 'build ' found at position 0.
Imports System.Text.RegularExpressions
Module RTL1Example
Public Sub Main()
Dim pattern As String = "\bb\w+\s"
Dim input As String = "build band tab"
For Each match As Match In Regex.Matches(input, pattern, RegexOptions.RightToLeft)
Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index)
Next
End Sub
End Module
' The example displays the following output:
' 'band ' found at position 6.
' 'build ' found at position 0.
è©äŸ¡é åº
RegexOptions.RightToLeft ãªãã·ã§ã³ã«ãã£ãŠãæ€çŽ¢æ¹åã倿Žãããæ£èŠè¡šçŸãã¿ãŒã³ãè©äŸ¡ãããé åºãéã«ãªããŸãã å³ããå·Šã®æ€çŽ¢ã§ã¯ãæ€çŽ¢ãã¿ãŒã³ãå³ããå·Šã«èªã¿åãããŸãã ãã£ãã㣠ã°ã«ãŒããåæ¹åç
§ãªã©ã«ã圱é¿ããå¯èœæ§ãããããã®åºå¥ã¯éèŠã§ãã ããšãã°ãåŒ Regex.Match("abcabc", @"\1(abc)", RegexOptions.RightToLeft) ã«ãã£ãŠäžèŽ abcabc ãæ€çŽ¢ãããŸãããå·Šããå³ã®æ€çŽ¢ (Regex.Match("abcabc", @"\1(abc)", RegexOptions.None)) ã§ã¯äžèŽã¯èŠã€ãããŸããã äžèŽãèŠã€ããããã«ã¯ã(abc) èŠçŽ ããçªå·ä»ããã£ãã㣠ã°ã«ãŒãèŠçŽ (\1) ãããå
ã«è©äŸ¡ããå¿
èŠãããããã§ãã
å èªã¿ã¢ãµãŒã·ã§ã³ãšåŸèªã¿ã¢ãµãŒã·ã§ã³
å
èªã¿ã¢ãµãŒã·ã§ã³ ((?=subexpression)) ãŸãã¯åŸèªã¿ã¢ãµãŒã·ã§ã³ ((?<=subexpression)) ã®äžèŽäœçœ®ã¯ãå³ããå·Šãžã®æ€çŽ¢ã§ã¯å€ãããŸããã å
èªã¿ã¢ãµãŒã·ã§ã³ã¯ãçŸåšã®äžèŽäœçœ®ã®å³åŽãåç
§ããŸããåŸèªã¿ã¢ãµãŒã·ã§ã³ã¯ãçŸåšã®äžèŽäœçœ®ã®å·ŠåŽãåç
§ããŸãã
ãã³ã
æ€çŽ¢ãå³ããå·Šãã©ããã«é¢ä¿ãªããã«ãã¯ããã€ã³ãã¯ãçŸåšã®ãããäœçœ®ããå§ãŸãå³ããå·Šæ€çŽ¢ã䜿çšããŠå®è£ ãããŸãã
ããšãã°ãæ£èŠè¡šçŸ (?<=\d{1,2}\s)\w+,\s\d{4} ã¯åŸèªã¿ã¢ãµãŒã·ã§ã³ã䜿çšããŠãæã®ååã®åã«ããæ¥ä»ããã¹ãããŸãã æ¬¡ã«ããã®æ£èŠè¡šçŸã¯ãæãšå¹Žãç
§åããŠããŸãã å
èªã¿ã¢ãµãŒã·ã§ã³ãšåŸèªã¿ã¢ãµãŒã·ã§ã³ã«ã€ããŠè©³ããã¯ããã°ã«ãŒãåæ§æäœããã芧ãã ããã
using System;
using System.Text.RegularExpressions;
public class RTL2Example
{
public static void Main()
{
string[] inputs = { "1 May, 1917", "June 16, 2003" };
string pattern = @"(?<=\d{1,2}\s)\w+,\s\d{4}";
foreach (string input in inputs)
{
Match match = Regex.Match(input, pattern, RegexOptions.RightToLeft);
if (match.Success)
Console.WriteLine($"The date occurs in {match.Value}.");
else
Console.WriteLine($"{input} does not match.");
}
}
}
// The example displays the following output:
// The date occurs in May, 1917.
// June 16, 2003 does not match.
Imports System.Text.RegularExpressions
Module RTL2Example
Public Sub Main()
Dim inputs() As String = {"1 May, 1917", "June 16, 2003"}
Dim pattern As String = "(?<=\d{1,2}\s)\w+,\s\d{4}"
For Each input As String In inputs
Dim match As Match = Regex.Match(input, pattern, RegexOptions.RightToLeft)
If match.Success Then
Console.WriteLine("The date occurs in {0}.", match.Value)
Else
Console.WriteLine("{0} does not match.", input)
End If
Next
End Sub
End Module
' The example displays the following output:
' The date occurs in May, 1917.
' June 16, 2003 does not match.
æ£èŠè¡šçŸãã¿ãŒã³ã¯ã次ã®è¡šã«ç€ºãããã«å®çŸ©ãããŠããŸãã
| ãã¿ãŒã³ | 説æ |
|---|---|
(?<=\d{1,2}\s) |
ãããã®éå§åã«ã1æ¡ãŸãã¯2æ¡ã®10鲿°ã®æ°åãšç¶ã空çœã眮ããªããã°ãªããŸããã |
\w+ |
1 ã€ä»¥äžã®åèªæåã«äžèŽããŸãã |
, |
1ã€ã®ã³ã³ãæåã«äžèŽãããŸãã |
\s |
ç©ºçœæåãšäžèŽããŸãã |
\d{4} |
4 æ¡ã®10鲿°ããããããŸãã |
ECMAScript ã®ãããã³ã°åäœ
æ¢å®ã§ã¯ãæ£èŠè¡šçŸãã¿ãŒã³ãå ¥åããã¹ãã«ç §åãããšãã«ãæ£èŠè¡šçŸãšã³ãžã³ã¯æšæºã®åäœã䜿çšããŸãã ãã ããRegexOptions.ECMAScript ãªãã·ã§ã³ã䜿çšããããšã§ãECMAScript äžèŽã®åäœã䜿çšããããã«æ£èŠè¡šçŸãšã³ãžã³ã«æç€ºã§ããŸãã
泚æ
ECMAScript æºæ ã®åäœã䜿çšããã«ã¯ãRegexOptions.ECMAScript å€ã options ã¯ã©ã¹ ã³ã³ã¹ãã©ã¯ã¿ãŒãŸãã¯éçãã¿ãŒã³äžèŽã¡ãœããã® Regex ãã©ã¡ãŒã¿ãŒã«æž¡ãå¿
èŠããããŸãã ã€ã³ã©ã€ã³ ãªãã·ã§ã³ãšããŠã¯äœ¿çšã§ããŸããã
RegexOptions.ECMAScript ãªãã·ã§ã³ã¯ãRegexOptions.IgnoreCaseãRegexOptions.Multilineãããã³RegexOptions.Compiledãªãã·ã§ã³ãšã®ã¿çµã¿åãããããšãã§ããŸãã ãã以å€ã®ãªãã·ã§ã³ãšåæã«æ£èŠè¡šçŸã§äœ¿çšãããšãArgumentOutOfRangeException ãçºçããŸãã
ECMAScript ãšæšæºæ£èŠè¡šçŸã¯ãæåã¯ã©ã¹ã®æ§æãèªå·±åç §ãã£ãã㣠ã°ã«ãŒããããã³ 8 鲿°ãšåæ¹åç §ã®è§£éãšãã 3 ã€ã®ç¹ã§åäœãç°ãªããŸãã
æåã¯ã©ã¹ã®æ§æã æšæºæ£èŠè¡šçŸã Unicode ããµããŒãããŠããã®ã«å¯ŸããŠãECMAScript 㯠Unicode ããµããŒãããŠããªãã®ã§ãECMAScript ã®æåã¯ã©ã¹ã®æ§æã«ã¯å¶éãå€ããæåã¯ã©ã¹ã®èšèªèŠçŽ ã«ãã£ãŠã¯æå³ãç°ãªããŸãã ããšãã° ECMAScript ã¯ãUnicode ã«ããŽãªããããã¯èŠçŽ (
\pããã³\P) ãªã©ã®èšèªèŠçŽ ããµããŒãããŠããŸããã åæ§ã«ãåèªæåãšäžèŽãã\wèŠçŽ ã¯ãECMAScript ã䜿çšããå Žåã¯[a-zA-Z_0-9]æåã¯ã©ã¹ãšç䟡ã«ãªããæšæºã®åäœã䜿çšããå Žåã¯[\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Nd}\p{Pc}\p{Lm}]ãšç䟡ã«ãªããŸãã 詳ããã¯ããæåã¯ã©ã¹ããã芧ãã ãããæ¬¡ã®äŸã¯ãæšæºãã¿ãŒã³äžèŽãš ECMAScript ãã¿ãŒã³äžèŽã®éãã瀺ããŠããŸãã ãã®äŸã§ã¯ãåèªãšããã«ç¶ãç©ºçœæåãšäžèŽããæ£èŠè¡šçŸ
\b(\w+\s*)+ãå®çŸ©ããŠããŸãã å ¥å㯠2 ã€ã®æååã§æ§æãããäžæ¹ã®æååã§ã¯ã©ãã³èªæåã»ããã䜿çšãããããäžæ¹ã®æååã§ã¯ããªã«æåã»ããã䜿çšãããŠããŸãã åºåçµæã瀺ãããã«ãECMAScript äžèŽã䜿çšãã Regex.IsMatch(String, String, RegexOptions) ã¡ãœããã®åŒã³åºãã§ã¯ããªã«æåã®åèªãç §åãããªãã®ã«å¯ŸããŠãæšæºäžèŽã䜿çšããã¡ãœããã®åŒã³åºãã§ã¯ããªã«æåã®åèªãç §åãããŠããŸããusing System; using System.Text.RegularExpressions; public class EcmaScriptExample { public static void Main() { string[] values = { "ÑелÑй ЌОÑ", "the whole world" }; string pattern = @"\b(\w+\s*)+"; foreach (var value in values) { Console.Write("Canonical matching: "); if (Regex.IsMatch(value, pattern)) Console.WriteLine($"'{value}' matches the pattern."); else Console.WriteLine($"{value} does not match the pattern."); Console.Write("ECMAScript matching: "); if (Regex.IsMatch(value, pattern, RegexOptions.ECMAScript)) Console.WriteLine($"'{value}' matches the pattern."); else Console.WriteLine($"{value} does not match the pattern."); Console.WriteLine(); } } } // The example displays the following output: // Canonical matching: 'ÑелÑй ЌОÑ' matches the pattern. // ECMAScript matching: ÑелÑй ÐŒÐžÑ does not match the pattern. // // Canonical matching: 'the whole world' matches the pattern. // ECMAScript matching: 'the whole world' matches the pattern.Imports System.Text.RegularExpressions Module Ecma1Example Public Sub Main() Dim values() As String = {"ÑелÑй ЌОÑ", "the whole world"} Dim pattern As String = "\b(\w+\s*)+" For Each value In values Console.Write("Canonical matching: ") If Regex.IsMatch(value, pattern) Then Console.WriteLine("'{0}' matches the pattern.", value) Else Console.WriteLine("{0} does not match the pattern.", value) End If Console.Write("ECMAScript matching: ") If Regex.IsMatch(value, pattern, RegexOptions.ECMAScript) Then Console.WriteLine("'{0}' matches the pattern.", value) Else Console.WriteLine("{0} does not match the pattern.", value) End If Console.WriteLine() Next End Sub End Module ' The example displays the following output: ' Canonical matching: 'ÑелÑй ЌОÑ' matches the pattern. ' ECMAScript matching: ÑелÑй ÐŒÐžÑ does not match the pattern. ' ' Canonical matching: 'the whole world' matches the pattern. ' ECMAScript matching: 'the whole world' matches the pattern.èªå·±åç §ãã£ãã㣠ã°ã«ãŒãã æ£èŠè¡šçŸãã£ãã㣠ã¯ã©ã¹ããããèªäœãžã®åæ¹åç §ãæã£ãŠããå Žåã¯ããã£ããã£ã®å埩åŠçã®ãã³ã«æŽæ°ããå¿ èŠããããŸãã æ¬¡ã®äŸã«ç€ºãããã«ããã®æ©èœã§ã¯ãæ£èŠè¡šçŸ
((a+)(\1) ?)+ã«ããå ¥åæåå " aa aaaa aaaaaa "ãšã®ç §åã¯ãECMAScript ã䜿çšããå Žåã¯æå¹ã«ãªããæšæºäžèŽã䜿çšããå Žåã¯ç¡å¹ã«ãªããŸããusing System; using System.Text.RegularExpressions; public class EcmaScript2Example { static string pattern; public static void Main() { string input = "aa aaaa aaaaaa "; pattern = @"((a+)(\1) ?)+"; // Match input using canonical matching. AnalyzeMatch(Regex.Match(input, pattern)); // Match input using ECMAScript. AnalyzeMatch(Regex.Match(input, pattern, RegexOptions.ECMAScript)); } private static void AnalyzeMatch(Match m) { if (m.Success) { Console.WriteLine($"'{pattern}' matches {m.Value} at position {m.Index}."); int grpCtr = 0; foreach (Group grp in m.Groups) { Console.WriteLine($" {grpCtr}: '{grp.Value}'"); grpCtr++; int capCtr = 0; foreach (Capture cap in grp.Captures) { Console.WriteLine($" {capCtr}: '{cap.Value}'"); capCtr++; } } } else { Console.WriteLine("No match found."); } Console.WriteLine(); } } // The example displays the following output: // No match found. // // '((a+)(\1) ?)+' matches aa aaaa aaaaaa at position 0. // 0: 'aa aaaa aaaaaa ' // 0: 'aa aaaa aaaaaa ' // 1: 'aaaaaa ' // 0: 'aa ' // 1: 'aaaa ' // 2: 'aaaaaa ' // 2: 'aa' // 0: 'aa' // 1: 'aa' // 2: 'aa' // 3: 'aaaa ' // 0: '' // 1: 'aa ' // 2: 'aaaa 'Imports System.Text.RegularExpressions Module Ecma2Example Dim pattern As String Public Sub Main() Dim input As String = "aa aaaa aaaaaa " pattern = "((a+)(\1) ?)+" ' Match input using canonical matching. AnalyzeMatch(Regex.Match(input, pattern)) ' Match input using ECMAScript. AnalyzeMatch(Regex.Match(input, pattern, RegexOptions.ECMAScript)) End Sub Private Sub AnalyzeMatch(m As Match) If m.Success Then Console.WriteLine("'{0}' matches {1} at position {2}.", pattern, m.Value, m.Index) Dim grpCtr As Integer = 0 For Each grp As Group In m.Groups Console.WriteLine(" {0}: '{1}'", grpCtr, grp.Value) grpCtr += 1 Dim capCtr As Integer = 0 For Each cap As Capture In grp.Captures Console.WriteLine(" {0}: '{1}'", capCtr, cap.Value) capCtr += 1 Next Next Else Console.WriteLine("No match found.") End If Console.WriteLine() End Sub End Module ' The example displays the following output: ' No match found. ' ' '((a+)(\1) ?)+' matches aa aaaa aaaaaa at position 0. ' 0: 'aa aaaa aaaaaa ' ' 0: 'aa aaaa aaaaaa ' ' 1: 'aaaaaa ' ' 0: 'aa ' ' 1: 'aaaa ' ' 2: 'aaaaaa ' ' 2: 'aa' ' 0: 'aa' ' 1: 'aa' ' 2: 'aa' ' 3: 'aaaa ' ' 0: '' ' 1: 'aa ' ' 2: 'aaaa 'æ£èŠè¡šçŸã¯ã次ã®è¡šã«ç€ºãããã«å®çŸ©ãããŠããŸãã
ãã¿ãŒã³ 説æ (a+) æå "a" ãš 1 å以äžäžèŽããŸãã ããã 2 çªç®ã®ãã£ãã㣠ã°ã«ãŒãã§ãã (\1) æåã®ãã£ãã㣠ã°ã«ãŒãã«ãã£ãŠãã£ããã£ãããéšåæååãšäžèŽããŸãã ããã 3 çªç®ã®ãã£ãã㣠ã°ã«ãŒãã§ãã ïŒ 0 åãŸã㯠1 åã®ç©ºçœæåãšäžèŽããŸãã ((a+)(\1) ?)+ 1 æå以äžã® "a"ãæåã®ãã£ãã㣠ã°ã«ãŒããšäžèŽããæååãããã³ 0 åãŸã㯠1 åã®ç©ºçœæåã 1 å以äžç¶ããã¿ãŒã³ãšäžèŽããŸãã ãããæåã®ãã£ãã㣠ã°ã«ãŒãã§ãã 8鲿°ãšã¹ã±ãŒããšåŸæ¹åç §ã®ãããŸããã®è§£æ±ºã æšæºæ£èŠè¡šçŸãš ECMAScript æ£èŠè¡šçŸã«ãã 8 鲿°ãšåæ¹åç §ã®è§£éã®éãã®æŠèŠã次ã®è¡šã«ç€ºããŸãã
æ£èŠè¡šçŸ æšæºã®åäœ ECMAScript ã®åäœ \0ã®åŸã« 0 ïœ 2 æ¡ã® 8 鲿°åãç¶ãå Žå8鲿°ãšããŠè§£éããã ããšãã°ã \044ã¯åžžã« 8 鲿°å€ãšããŠè§£éããã"$" ãæå³ããŸããåãåäœã§ãã \ã®åŸã« 1 ïœ 9 ã®æ°åãç¶ãããã®åŸã« 10 鲿°åãç¶ããªãå ŽååŸæ¹åç §ãšããŠè§£éããŸãã ããšãã°ã9 çªç®ã®ãã£ãã㣠ã°ã«ãŒããååšããªãå Žåã§ãã \9ã¯åžžã«åŸæ¹åç § 9 ã§ãã ãã£ãã㣠ã°ã«ãŒããååšããªãå Žåã¯ãæ£èŠè¡šçŸããŒãµãŒã¯ ArgumentException ãã¹ããŒããŸããåäžã® 10 鲿°åã®ãã£ãã㣠ã°ã«ãŒããååšããå Žåã¯ããã®æ°åãžã®åæ¹åç §ã§ãã ãã以å€ã®å Žåã¯ãå€ã¯ãªãã©ã«ãšããŠè§£éãããŸãã \ã®åŸã« 1 ïœ 9 ã®æ°åãç¶ãããã®åŸã« 10 鲿°åãç¶ãå Žåæ°å㯠10 鲿°å€ãšããŠè§£éãããŸãã ãã®ãã£ãã㣠ã°ã«ãŒããååšããå Žåã¯ãåŒã¯åŸæ¹åç §ãšããŠè§£éãããŸãã
ãã以å€ã®å Žåã¯ãå è¡ããæ°åã 377 ãŸã§ã®ç¯å²ã§ 8 鲿°ãšããŠè§£éãããŸããã€ãŸããå€ã®äžäœ 8 ãããã®ã¿ãåŠçã®å¯Ÿè±¡ã«ãªããŸãã æ®ãã®æ°åã¯ãªãã©ã«ãšããŠè§£éãããŸãã ããšãã°ãåŒ\3000ã§ã¯ããã£ãã㣠ã°ã«ãŒã 300 ãååšããå Žåã¯åæ¹åç § 300 ãšããŠè§£éãããŸãããã£ãã㣠ã°ã«ãŒã 300 ãååšããªãå Žå㯠8 鲿° 300 ãšããã«ç¶ã 0 ãšããŠè§£éãããŸãããã£ããã£ãåç §ã§ããç¯å²ã§ãã§ããã ãå€ãã®æ°åã 10 鲿°ã«å€æããåŸæ¹åç §ãšããŠè§£éããŸãã 倿ã§ããæ°åããªãå Žåã¯ãå è¡ããæ°åã 377 ãŸã§ã®ç¯å²ã§ 8 鲿°ãšããŠè§£éãããæ®ãã®æ°åã¯ãªãã©ã«ãšããŠè§£éãããŸãã
ã€ã³ããªã¢ã³ã ã«ã«ãã£ã䜿çšããæ¯èŒ
æ£èŠè¡šçŸãšã³ãžã³ã¯ãæ¢å®ã§ã¯å€§æåãšå°æåãåºå¥ããªãæ¯èŒãè¡ãéãçŸåšã®ã«ã«ãã£ã®è¡šèšèŠåãçšããŠã倧æåãšå°æåã®ç䟡æ§ã倿ããŸãã
ãã ãããã®åäœã¯ãæ¯èŒã®çš®é¡ã«ãã£ãŠã¯æãŸãããªãå ŽåããããŸããå
·äœçãªäŸãšããŠã¯ããŠãŒã¶ãŒå
¥åãšããã¹ã¯ãŒãããã¡ã€ã«ãURL ãªã©ãã·ã¹ãã ãªãœãŒã¹ã®ååãæ¯èŒããå ŽåãæããããŸãã ãã®å Žåã«è©²åœããäŸã次ã«ç€ºããŸãã ãã®ã³ãŒãã¯ãURL ã FILE:// ã§å§ãŸããä»»æã®ãªãœãŒã¹ãžã®ã¢ã¯ã»ã¹ããããã¯ããããšãç®çãšããŠããŸãã
$FILE:// ãšããæ£èŠè¡šçŸã䜿çšããŠãæååã«å¯Ÿãã倧æåãšå°æåãåºå¥ããªãäžèŽã詊ã¿ãããŸãã ãã ããçŸåšã®ã·ã¹ãã ã«ã«ãã£ã tr-TR (ãã«ã³èª (ãã«ã³)) ã§ããå Žåã"I" 㯠"i" ã®å€§æå衚çŸãšã¯ç䟡ã§ã¯ãããŸããã çµæãšããŠãRegex.IsMatch ã¡ãœããã®åŒã³åºãã§ã¯ãfalse ãè¿ããããã¡ã€ã«ãžã®ã¢ã¯ã»ã¹ã¯èš±å¯ãããŸãã
CultureInfo defaultCulture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = new CultureInfo("tr-TR");
string input = "file://c:/Documents.MyReport.doc";
string pattern = "FILE://";
Console.WriteLine($"Culture-sensitive matching ({Thread.CurrentThread.CurrentCulture.Name} culture)...");
if (Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase))
Console.WriteLine("URLs that access files are not allowed.");
else
Console.WriteLine($"Access to {input} is allowed.");
Thread.CurrentThread.CurrentCulture = defaultCulture;
// The example displays the following output:
// Culture-sensitive matching (tr-TR culture)...
// Access to file://c:/Documents.MyReport.doc is allowed.
Dim defaultCulture As CultureInfo = Thread.CurrentThread.CurrentCulture
Thread.CurrentThread.CurrentCulture = New CultureInfo("tr-TR")
Dim input As String = "file://c:/Documents.MyReport.doc"
Dim pattern As String = "$FILE://"
Console.WriteLine("Culture-sensitive matching ({0} culture)...",
Thread.CurrentThread.CurrentCulture.Name)
If Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase) Then
Console.WriteLine("URLs that access files are not allowed.")
Else
Console.WriteLine("Access to {0} is allowed.", input)
End If
Thread.CurrentThread.CurrentCulture = defaultCulture
' The example displays the following output:
' Culture-sensitive matching (tr-TR culture)...
' Access to file://c:/Documents.MyReport.doc is allowed.
泚æ
倧æåãšå°æåãåºå¥ããæå忝èŒãšã€ã³ããªã¢ã³ã ã«ã«ãã£ã䜿çšããæå忝èŒã®è©³çްã«ã€ããŠã¯ããæååã䜿çšããããã®ãã¹ã ãã©ã¯ãã£ã¹ããåç §ããŠãã ããã
çŸåšã®ã«ã«ãã£ã§å€§æåãšå°æåãåºå¥ããªãæ¯èŒãè¡ã代ããã«ãRegexOptions.CultureInvariant ãªãã·ã§ã³ãæå®ãããšãèšèªã®ã«ã«ãã£ã®éããç¡èŠããŠãã€ã³ããªã¢ã³ã ã«ã«ãã£ã®èŠåã䜿çšã§ããŸãã
泚æ
ã€ã³ããªã¢ã³ã ã«ã«ãã£ã䜿çšããŠæ¯èŒãè¡ãã«ã¯ãRegexOptions.CultureInvariant å€ã options ã¯ã©ã¹ ã³ã³ã¹ãã©ã¯ã¿ãŒãŸãã¯éçãã¿ãŒã³äžèŽã¡ãœããã® Regex ãã©ã¡ãŒã¿ãŒã«æž¡ãå¿
èŠããããŸãã ã€ã³ã©ã€ã³ ãªãã·ã§ã³ãšããŠã¯äœ¿çšã§ããŸããã
次ã®äŸã¯åã®äŸãšåããã®ã§ãããRegex.IsMatch(String, String, RegexOptions) ãå«ããªãã·ã§ã³ãæå®ããŠãéçã¡ãœãã RegexOptions.CultureInvariant ãåŒã³åºãããŠããç¹ãç°ãªããŸãã çŸåšã®ã«ã«ãã£ããã«ã³èª (ãã«ã³) ã«èšå®ãããŠããå Žåã§ããæ£èŠè¡šçŸãšã³ãžã³ã§ã¯ "FILE" ãš "file" ãæ£åžžã«äžèŽãããã¡ã€ã« ãªãœãŒã¹ãžã®ã¢ã¯ã»ã¹ãæåŠãããŸãã
CultureInfo defaultCulture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = new CultureInfo("tr-TR");
string input = "file://c:/Documents.MyReport.doc";
string pattern = "FILE://";
Console.WriteLine("Culture-insensitive matching...");
if (Regex.IsMatch(input, pattern,
RegexOptions.IgnoreCase | RegexOptions.CultureInvariant))
Console.WriteLine("URLs that access files are not allowed.");
else
Console.WriteLine($"Access to {input} is allowed.");
Thread.CurrentThread.CurrentCulture = defaultCulture;
// The example displays the following output:
// Culture-insensitive matching...
// URLs that access files are not allowed.
Dim defaultCulture As CultureInfo = Thread.CurrentThread.CurrentCulture
Thread.CurrentThread.CurrentCulture = New CultureInfo("tr-TR")
Dim input As String = "file://c:/Documents.MyReport.doc"
Dim pattern As String = "$FILE://"
Console.WriteLine("Culture-insensitive matching...")
If Regex.IsMatch(input, pattern,
RegexOptions.IgnoreCase Or RegexOptions.CultureInvariant) Then
Console.WriteLine("URLs that access files are not allowed.")
Else
Console.WriteLine("Access to {0} is allowed.", input)
End If
Thread.CurrentThread.CurrentCulture = defaultCulture
' The example displays the following output:
' Culture-insensitive matching...
' URLs that access files are not allowed.
éããã¯ãã©ããã³ã° ã¢ãŒã
æ¢å®ã§ã¯ã.NET ã®æ£èŠè¡šçŸãšã³ãžã³ã¯ããã¿ãŒã³ã®äžèŽãæ€çŽ¢ããããã« backtracking ã䜿çšããŸãã ããã¯ãã©ããã³ã° ãšã³ãžã³ã¯ã1 ã€ã®ãã¿ãŒã³ãäžèŽãããããšãããšã³ãžã³ã§ãããããã倱æããå Žåã¯ãæ»ã£ãŠä»£æ¿ãã¿ãŒã³ãšã®äžèŽã詊ã¿ãŸãã ããã¯ãã©ããã³ã° ãšã³ãžã³ã¯äžè¬çãªã±ãŒã¹ã§ã¯éåžžã«é«éã§ããããã¿ãŒã³ãå€ããåæ°ãå¢ãããšé床ãäœäžããèŽåœçãªããã¯ãã©ããã³ã°ã«ã€ãªããå¯èœæ§ããããŸãã .NET 7 ã§å°å ¥ããã RegexOptions.NonBacktracking ãªãã·ã§ã³ã¯ããã¯ãã©ããã³ã°ã䜿çšãããææªã®ã·ããªãªãåé¿ããŸãã ãã®ç®æšã¯ãæ€çŽ¢ãããå ¥åã«é¢ä¿ãªããäžè²«ããŠè¯å¥œãªåäœãæäŸããããšã§ãã
Warning
.NETæ£èŠè¡šçŸãšã³ãžã³ã¯ããã¿ãŒã³ãä¿¡é ŒãããŠããããšãåæãšããŠããŸãã éããã¯ãã©ããã³ã° ã¢ãŒãã§ã¯ããã®åæã¯å€ãããŸãããããã¯ãã¢ã¯ãã£ãã«æªæã®ãããã¿ãŒã³ããã§ã¯ãªããã³ã¹ãã®é«ã å ¥åããä¿è· ããŸãã 詳现ã«ã€ããŠã¯ãã ä¿¡é Œã§ãããã¿ãŒã³ã䜿çšããããåç §ããŠãã ããã
RegexOptions.NonBacktracking ãªãã·ã§ã³ã§ã¯ãä»ã®çµã¿èŸŒã¿ãšã³ãžã³ã§ãµããŒãããããã¹ãŠã®ãã®ããµããŒããããŠããããã§ã¯ãããŸããã ç¹ã«ããã®ãªãã·ã§ã³ã¯ã RegexOptions.RightToLeftã RegexOptions.ECMAScriptããŸã㯠RegexOptions.AnyNewLineãšçµã¿åãããŠäœ¿çšããããšã¯ã§ããŸããã ãŸãããã¿ãŒã³ã§æ¬¡ã®ã³ã³ã¹ãã©ã¯ããèš±å¯ãããŸããã
- ã¢ããã㯠ã°ã«ãŒã
- éåç §
- ã°ã«ãŒãã®ãã©ã³ã¹èª¿æŽ
- æ¡ä»¶åŒ
- ã«ãã¯ã¢ã©ãŠã³ã (Lookarounds)
- éå§ã¢ã³ã«ãŒ (
\G)
ãŸã RegexOptions.NonBacktracking ã«ã¯ãå®è¡ã«é¢ããŠåŸ®åŠãªéãããããŸãã ãã£ãã㣠ã°ã«ãŒããã«ãŒãå ã«ããå Žåãã»ãšãã©ã® (.NET以å€ã®) æ£èŠè¡šçŸãšã³ãžã³ã¯ããã®ãã£ããã£ã«å¯ŸããŠæåŸã«äžèŽããå€ã®ã¿ãæäŸããŸãã ãã ãã.NETã®æ£èŠè¡šçŸãšã³ãžã³ã¯ãã«ãŒãå ã§ãã£ããã£ããããã¹ãŠã®å€ã远跡ãããããã«ã¢ã¯ã»ã¹ã§ããããã«ããŸãã RegexOptions.NonBacktracking ãªãã·ã§ã³ã¯ãä»ã®ã»ãšãã©ã®æ£èŠè¡šçŸã®å®è£ ãšåãã§ãããæçµçãªãã£ããã£ã®æäŸã®ã¿ããµããŒãããŸãã
ããã¯ãã©ããã³ã°ã®è©³çްã«ã€ããŠã¯ããæ£èŠè¡šçŸã«ãããããã¯ãã©ããã³ã°ããåç §ããŠãã ããã
AnyNewLine ã¢ãŒã
æ¢å®ã§ã¯ã.NETã®æ£èŠè¡šçŸãšã³ãžã³ã¯ã\n ã®ã¿ãæ¹è¡æåãšããŠæ±ããŸãã ã¢ã³ã«ãŒã® ^ ãš $ ( RegexOptions.Multiline ã¢ãŒã)ã \Zãã¯ã€ã«ãã«ãŒã . ã¯ãã¹ãŠã \n ãå¯äžã®ç·ã®å¢çãšããŠäœ¿çšããŸãã ã€ãŸãã$ ã¯ã\r\n (Windows ã¹ã¿ã€ã«ã®è¡ã®çµãã) ã®åã«äžèŽããã. 㯠\r ãšäžèŽããŸããã\n (RegexOptions.Singleline ãæå¹ã«ãªã£ãŠããªãå Žå) ãã®å Žåã. ã¯ãã¹ãŠã®æåãšäžèŽããŸã)ãããã¯ãUnix 以å€ã®è¡æ«ãæ··åšããããã¹ããåŠçãããšãã«äžè¬çãªãã°ã«ã€ãªãããŸãã
.NET 11 ã§å°å
¥ããã RegexOptions.AnyNewLine ãªãã·ã§ã³ ãããã®ã³ã³ã¹ãã©ã¯ãã¯ã\r\n (CR+LF)ã\r (CR)ã\n (LF)ã\u0085 (NEL)ã\u2028 (LS)ã\u2029 (PS) ã®ãã¹ãŠã®äžè¬çãªæ¹è¡ã·ãŒã±ã³ã¹ãèªèããŸãã ãã㯠Unicode TR18 RL1.6 ãšäžèŽããŸãã
ããšãã°ãAnyNewLineã䜿çšããªãå Žåãæååå
ã®è¡ãWindowsè¡æ«ãšäžèŽãããã«ã¯ã\r?$ ãªã©ã®æåã«ããåé¿çãå¿
èŠã§ãã
// BUG: .+$ captures trailing \r on Windows line endings
var match = Regex.Match("foo\r\nbar", @".+$", RegexOptions.Multiline);
Console.WriteLine(match.Value); // "foo\r" -- not "foo"!
AnyNewLineã§ã¯ãã¢ã³ã«ãŒã¯ãã¹ãŠã®æ¹è¡ã¿ã€ããèªåçã«åŠçããŸãã
var match = Regex.Match("foo\r\nbar", @".+$",
RegexOptions.Multiline | RegexOptions.AnyNewLine);
Console.WriteLine(match.Value); // "foo"
次ã®è¡šã¯ã AnyNewLine ãåã³ã³ã¹ãã©ã¯ãã«äžãã圱é¿ããŸãšãããã®ã§ãã
| æ§é äœ | æ¢å®ã®åäœ |
AnyNewLine ãã |
|---|---|---|
. (æ¢å®å€) |
ãé€ãä»»æã®æåãšäžèŽããŸãã \n |
\rã\nã\u0085ã\u2028ã\u2029以å€ã®æåã«äžèŽãã |
$ (è€æ°è¡) |
åã®äžèŽ \n |
\r\nã\rã\nã\u0085ã\u2028ã\u2029 ã®åã«äžèŽããŸã |
^ (è€æ°è¡) |
\n ã®åŸã«äžèŽããŸã |
\r\nã\rã\nã\u0085ã\u2028ã\u2029ã®åŸã«äžèŽãããã® |
$ (æ¢å®å€) / \Z |
æååã®æ«å°Ÿã§ \n ã®åã«äžèŽããŸã |
æååã®æ«å°Ÿã«ããæ¹è¡ã·ãŒã±ã³ã¹ã®çŽåã«ãã¹ãŠäžèŽããŸã |
äž»èŠãªèšèšãã€ã³ã:
-
\r\nã¯ã¢ãããã¯ã«æ±ãããŸãã$å®å šãª\r\nã·ãŒã±ã³ã¹ã®åã«äžèŽãã\rãš\nã®éã§ã¯äžèŽããŸããã -
Singlelineãåªå ãããŸãã.ãšSinglelineã®äž¡æ¹ãæã€AnyNewLineã¯ããã¹ãŠã®æå (æ¹è¡ãå«ã) ãšäžèŽããSinglelineã®æ¢åã®åäœãšäžèŽããŸãã -
\Aãš\zã¯åœ±é¿ãåããŸããã絶察æååéå§ã¢ã³ã«ãŒãšæååæ«å°Ÿã¢ã³ã«ãŒã¯å€æŽãããŸããã -
äºææ§ã®ãªããªãã·ã§ã³:
AnyNewLineã RegexOptions.NonBacktracking ãŸã㯠RegexOptions.ECMAScriptãšçµã¿åãããããšã¯ã§ããŸããã 倿ã詊ã¿ããšãArgumentOutOfRangeException ãã¹ããŒãããŸãã