在正则表达式句子中使用':'(0x3A)字符?

时间:2021-09-16 23:37:18

No, this is not a replicate of The ':' character, hexadecimal value 0x3A, cannot be included in a name

不,这不是':'字符的复制品,十六进制值0x3A,不能包含在名称中

I have a Regex for my syntax highlighter for my scripting language:

对于我的脚本语言,我有一个用于语法高亮显示的正则表达式:

`@"\bClass+(?<range>\w+?)\b"` 

which basically marks Class Name

这基本上标志着班级名称

(with the engine I got online)

(我上网的引擎)

I'm no master in Regex but for some reason the : character that uses my language to create labels - doesn't work.

我在正则表达式中不是主人,但由于某种原因:使用我的语言来创建标签的字符 - 不起作用。

I tried

我试过了

 @"\b:+(?<range>\w+?)\b"`, `@"\b\:+(?<range>\w+?)\b"`<RB> `@"\b(\x3A)+(?<range>\w+?)\b"

And it refuses to work!

它拒绝工作!

Any ideas?

有任何想法吗?

1 个解决方案

#1


3  

I suspect the issue in your case is not the : itself, but the \b before it. \b marks the boundary between a word character and nonword character, but while Class is comprised of word characters, : is a nonword character. So \b is behaving differently for : than it would for Class, so:

我怀疑你的案子中的问题不是:本身,而是它之前的\ b。 \ b标记单词字符和非单词字符之间的边界,但是Class由单词字符组成,::是非单词字符。因此\ b的行为与以下行为不同,因为它与Class类似,因此:

`\bClass` matches " Class Name"
`\b:` does not match " : Name"

If you use your original expression but replace the first \b with (?<!\w), it may identify the : properly.

如果您使用原始表达式但将第一个\ b替换为(?<!\ w),则可以正确识别:

#1


3  

I suspect the issue in your case is not the : itself, but the \b before it. \b marks the boundary between a word character and nonword character, but while Class is comprised of word characters, : is a nonword character. So \b is behaving differently for : than it would for Class, so:

我怀疑你的案子中的问题不是:本身,而是它之前的\ b。 \ b标记单词字符和非单词字符之间的边界,但是Class由单词字符组成,::是非单词字符。因此\ b的行为与以下行为不同,因为它与Class类似,因此:

`\bClass` matches " Class Name"
`\b:` does not match " : Name"

If you use your original expression but replace the first \b with (?<!\w), it may identify the : properly.

如果您使用原始表达式但将第一个\ b替换为(?<!\ w),则可以正确识别: