允许特定和特殊字符的正则表达式

huangapple go评论77阅读模式
英文:

Regex to allow specific and special characters

问题

我需要在Java中创建一个正则表达式,允许以下内容:

字符a-z,A-Z和àèìòùÀÈÌÒÙáéíóúýÁÉÍÓÚÝâêîôûÂÊÎÔÛãñõÃÑÕäëïöüÿÄËÏÖÜŸ
数字0-9
特殊字符:@ - /
单词之间的空格

目前我有这个正则表达式:'^[a-zA-Z0-9\u00C0-\u024F\u1E00-\u1EFF]+$',它允许:a-z,A-Z,0-9和àèìòùÀÈÌÒÙáéíóúýÁÉÍÓÚÝâêîôûÂÊÎÔÛãñõÃÑÕäëïöüÿÄËÏÖÜŸ - 但不允许空格或字符@ - /

我无法找到一个包含所有这些内容的正则表达式,请帮忙。

我尝试了以下其他正则表达式变体:

'^[a-zA-Z0-9!/-\\u00C0-\\u024F\\u1E00-\\u1EFF]+$'
'^[a-zA-Z0-9\\w+\\u00C0-\\u024F\\u1E00-\\u1EFF]+$'
'^[a-zA-Z0-9\\w+@/-\\u00C0-\\u024F\\u1E00-\\u1EFF]+$'
英文:

I need to create a regex in java that allows the following:

Characters a-z, A-Z and àèìòùÀÈÌÒÙáéíóúýÁÉÍÓÚÝâêîôûÂÊÎÔÛãñõÃÑÕäëïöüÿÄËÏÖÜŸ
Numbers 0-9
Special characters: @ - /
Spaces between words

Currently I have this: '^[a-zA-Z0-9\u00C0-\u024F\u1E00-\u1EFF]+$'
which allows: a-z, A-Z, 0-9 and àèìòùÀÈÌÒÙáéíóúýÁÉÍÓÚÝâêîôûÂÊÎÔÛãñõÃÑÕäëïöüÿÄËÏÖÜŸ - but does not allow spaces or the characters @ - /
I cannot figure out a regex that includes all of this, please help

I tried the following other regex variations:

'^\[a-zA-Z0-9!/-\\u00C0-\\u024F\\u1E00-\\u1EFF\]+$'
'^\[a-zA-Z0-9\\w+\\u00C0-\\u024F\\u1E00-\\u1EFF\]+$'
'^\[a-zA-Z0-9\\w+@/-\\u00C0-\\u024F\\u1E00-\\u1EFF\]+$'

答案1

得分: 2

'^[-@/ a-zA-Z0-9\u00C0-\u024F\u1E00-\u1EFF]+$'

最好在字符类中首先添加`-`,以确保它不被解释为范围对于其他缺失的字符只需添加它们即可

还要注意您的范围`\u00C0-\u024F``\u1E00-\u1EFF`添加了比只有àèìòùÀÈÌÒÙáéíóúýÁÉÍÓÚÝâêîôûÂÊÎÔÛãñõÃÑÕäëïöüÿÄËÏÖÜŸ(并且范围在不同的语言环境下可能不太可靠;最好明确添加所有所需的字符)

而要匹配单词之间”,您可能想要使用`\b[...]\b`而不是`^[...]$`,后者匹配整个输入
英文:
^[-@/ a-zA-Z0-9\u00C0-\u024F\u1E00-\u1EFF]+$'

A - is best added first in a character class to ensure it is not interpreted as range. For the other missing characters simply add them.

Also note that your ranges \u00C0-\u024F and \u1E00-\u1EFF add a lot more than just àèìòùÀÈÌÒÙáéíóúýÁÉÍÓÚÝâêîôûÂÊÎÔÛãñõÃÑÕäëïöüÿÄËÏÖÜŸ (and ranges can be a bit unreliable with different locals; might be best to explicit add all wanted characters)

And to match "between words" you probably want \b[...]\b instead of ^[...]$ which matches the entire input.

答案2

得分: -2

这个正则表达式有效:

'^[a-zA-Z0-9\u00C0-\u024F\u1E00-\u1EFF\@\/\-\s]+$'
英文:

This worked:

'^[a-zA-Z0-9\u00C0-\u024F\u1E00-\u1EFF\@\/\-\s]+$'

huangapple
  • 本文由 发表于 2023年7月27日 16:52:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76778068.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定