简体   繁体   English

RegEx:\\ w - UTF-8中的“_”+“ - ”

[英]RegEx: \w - “_” + “-” in UTF-8

I need a regular expression that matches UTF-8 letters and digits, the dash sign ( - ) but doesn't match underscores ( _ ), I tried these silly attempts without success: 我需要一个匹配UTF-8字母和数字的正则表达式,短划线符号( - )但不匹配下划线( _ ),我尝试了这些愚蠢的尝试但没有成功:

  • ([\\w-^_])+
  • ([\\w^_]-?)+
  • (\\w[^_]-?)+

The \\w is shorthand for [A-Za-z0-9_] , but it also matches UTF-8 chars if I have the u modifier set. \\w[A-Za-z0-9_]简写,但如果我设置了u修饰符,它也匹配UTF-8字符。

Can anyone help me out with this one? 任何人都可以帮我解决这个问题吗?

Try this: 尝试这个:

(?:[\w\-](?<!_))+

It does a simple match on anything that is encoded as a \\w (or a dash) and then has a zero-width lookbehind that ensures that the character that was just matched is not a underscore. 它对编码为\\ w(或破折号)的任何内容进行简单匹配,然后具有零宽度的lookbehind,以确保刚刚匹配的字符不是下划线。

Otherwise you could pick this one: 否则你可以选择这个:

(?:[^_\W]|-)+

which is a more set-based approach (note the uppercase W) 这是一种更基于集合的方法(注意大写W)

OK, I had a lot of fun with unicode in php's flavor of PCREs :D Peekaboo says there is a simple solution available: 好吧,我在php的PCRE风格中使用unicode非常有趣:D Peekaboo说有一个简单的解决方案:

[\p{L}\p{N}\-]+

\\p{L} matches anything unicode that qualifies as a Letter (note: not a word character, thus no underscores), while \\p{N} matches anything that looks like a number (including roman numerals and more exotic things). \\ p {L}匹配任何符合字母条件的unicode(注意:不是单词字符,因此没有下划线),而\\ p {N}匹配任何看起来像数字的东西(包括罗马数字和更奇特的东西)。
\\- is just an escaped dash. \\ - 只是一个逃脱的破折号。 Although not strictly necessary, I tend to make it a point to escape dashes in character classes... Note, that there are dozens of different dashes in unicode, thus giving rise to the following version: 虽然不是绝对必要的,但我倾向于在字符类中使用破折号...注意,在unicode中有许多不同的破折号,因此产生以下版本:

[\p{L}\p{N}\p{Pd}]+

Where "Pd" is Punctuation Dash, including, but not limited to our minus-dash-thingy. 其中“Pd”是标点符号,包括但不限于我们的减号。 (Note, again no underscore here). (注意,这里再没有下划线)。

我不确定你使用哪种语言,但在PERL中你可以简单地写:[[:alnum:] - ] +当设置正确的语言环境时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM