英文:
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]+$'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论