如何为现有正则表达式添加韩文(Hangul)匹配范围?
Got it, let's get your regex updated to include Hangul characters!
First, the most common Hangul characters you'll want to match are Hangul Syllables—these are the full combined characters like 가, 나, 안녕—their Unicode range is U+AC00 to U+D7AF, which you can represent directly in regex as 가-힣 (C# supports Unicode characters in regex patterns when your file/string is properly encoded).
Updated Regex (Basic Hangul Support)
If you just need to match standard full Hangul syllables, modify your regex like this:
Regex.IsMatch(p.Name, @"^[a-zA-Z0-9ァ-ヾぁ-んー一-龠々가-힣]+$")
Full Hangul Support (Including Standalone Jamo)
If you also want to cover standalone Hangul Jamo (individual consonants/vowels like ㄱ, ㅏ, ㅑ), you'll need to add three additional Unicode ranges. These cover the building blocks of Hangul that aren't combined into full syllables:
- Hangul Jamo:
U+1100-U+11FF - Hangul Compatibility Jamo:
U+3130-U+318F - Hangul Jamo Extended-A:
U+A960-U+A97F
Here's the regex with complete Hangul coverage:
Regex.IsMatch(p.Name, @"^[a-zA-Z0-9ァ-ヾぁ-んー一-龠々가-힣\u1100-\u11FF\u3130-\u318F\uA960-\uA97F]+$")
Quick Testing Tip
To confirm it works as expected, test with sample strings:
- Valid (returns
true):"안녕하세요123ABC漢字カタカナ" - Invalid (returns
false):"안녕!하세요"(contains an exclamation mark outside your allowed set)
内容的提问来源于stack exchange,提问作者Goon

