.Net正規表現パターンを使用してC#識別子、具体的にはプロパティまたはフィールド名を照合する正しい方法は何ですか?
バックグラウンド。以前はASCII中心の@"[_a-zA-Z] [_ a-zA-Z0-9] *"を使用していましたが、現在はUnicodeの大文字と小文字が合法です(例: "AboöДЖem")。これらをパターンにどのように含める必要がありますか?
ありがとう、マックス
.Net正規表現パターンを使用してC#識別子、具体的にはプロパティまたはフィールド名を照合する正しい方法は何ですか?
バックグラウンド。以前はASCII中心の@"[_a-zA-Z] [_ a-zA-Z0-9] *"を使用していましたが、現在はUnicodeの大文字と小文字が合法です(例: "AboöДЖem")。これらをパターンにどのように含める必要がありますか?
ありがとう、マックス
許可されていない先頭の数字を考慮したバージョンは次のとおりです。
^(?:((?!\d)\w+(?:\.(?!\d)\w+)*)\.)?((?!\d)\w+)$
そして、PowerShellでのいくつかのテストは次のとおりです。
[regex]$regex = '(?x:
^ # Start of string
(?:
( # Namespace
(?!\d)\w+ # Top-level namespace
(?:\.(?!\d)\w+)* # Subsequent namespaces
)
\. # End of namespaces period
)? # Namespace is optional
((?!\d)\w+) # Class name
$ # End of string
)'
@(
'System.Data.Doohickey'
'_1System.Data.Doohickey'
'System.String'
'System.Data.SqlClient.SqlConnection'
'DoohickeyClass'
'Stackoverflow.Q4400348.AboöДЖem'
'1System.Data.Doohickey' # numbers not allowed at start of namespace
'System.Data.1Doohickey' # numbers not allowed at start of class
'global::DoohickeyClass' # "global::" not part of actual namespace
) | %{
($isMatch, $namespace, $class) = ($false, $null, $null)
if ($_ -match $regex) {
($isMatch, $namespace, $class) = ($true, $Matches[1], $Matches[2])
}
new-object PSObject -prop @{
'IsMatch' = $isMatch
'Name' = $_
'Namespace' = $namespace
'Class' = $class
}
} | ft IsMatch, Name, Namespace, Class -auto
http://msdn.microsoft.com/en-us/library/aa664670.aspxによると、キーワードとunicode-escape-sequenceのものを無視すると、
@?[_\p{Lu}\p{Ll}\p{Lt}\p{Lm}\p{Lo}\p{Nl}][\p{Lu}\p{Ll}\p{Lt}\p{Lm}\p{Lo}\p{Nl}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\p{Cf}]*
正規表現\wの事前定義されたクラスによって解決されるその問題は、öДと一致しますか。