How to make a regex to match string that starts with 0-9 or a-z with accents and must accept only this special character – _ ' between words?

huangapple 未分类评论46阅读模式
英文:

How to make a regex to match string that starts with 0-9 or a-z with accents and must accept only this special character - _ ' between words?

问题

以下是要翻译的内容:

我的模式必须匹配以下字符串:

 - 可以以数字开头
 - 可以以带重音或不带重音的字母开头
 - 不能以空格开头
 - 不能以特殊字符开头
 - 允许单词之间有空格
 - 除了 - _ ' 这些特殊字符外,不接受其他特殊字符

我的当前模式是:^[^_\W][\p{L}\s0-9À-ÖØ-öø-ÿ.'-]+$

有效示例:

> 引用
>
> João Antonio
>
> João-Antonio
>
> João's Company
>
> Peter Müller
>
> François Hollande
>
> Patrick O'Brian
>
> Silvana Koch-Mehrin

无效示例:

> Company N@me
>
> 100% Company
>
>  \Company
>
>  \s Company
>
> _Blockquote

请帮助我!
英文:

My pattern must match a String that:

  • Can start with number
  • Can start with letters with accents or without accents too
  • Can't start with spaces
  • Can't start with special characters
  • Allow spaces between words
  • Do not accept special character except: - _ '

My current patter is: ^[^_\W][\p{L}\s0-9À-ÖØ-öø-ÿ.'-]+$

Valid examples:

> Blockquote
>
> João Antonio
>
> João-Antonio
>
> João's Company
>
> Peter Müller
>
> François Hollande
>
> Patrick O'Brian
>
> Silvana Koch-Mehrin

Invalid examples:

> Company N@me
>
> 100% Company
>
> \Company
>
> \s Company
>
> _Blockquote

Please help me!

答案1

得分: 0

首字母:

- 以数字或字母开头
- 不包括重音符号和特殊字符

`[^\W_]`

文本的其余部分:

- 包括数字、字母和重音符号
- 包括 `_`、`-`、`'` 和空格

`[0-9]` & `[A-Za-zÀ-ÖØ-öø-ÿ]` & `[_\-\' ]`

在这里:

    ^[^\W_][0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\' ]+$

参考这个[问题][1]

当你需要处理复杂的正则表达式时,可以使用[Regexr'][2]!
  
  [1]: https://stackoverflow.com/questions/20690499/concrete-javascript-regex-for-accented-characters-diacritics
  [2]: https://regexr.com/
英文:

First letter:

  • Start with number, letter
  • Exclude accents and special chars

[^\W_]

Rest of the text:

  • Include number, letter and accents
  • Include _, -, ' and

[0-9] & [A-Za-zÀ-ÖØ-öø-ÿ] & [_\-\' ]

Here you are:

^[^\W_][0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\' ]+$

See this question

When you have to deal with complicated Regex, use the Regexr'!

答案2

得分: 0

我最好的是:

/^[^_\W][\p{L}\s0-9À-ÖØ-öø-ÿ.'-]+$/gi

测试:https://regexr.com/521r2

英文:

My best was:

/^[^_\W][\p{L}\s0-9À-ÖØ-öø-ÿ.'-]+$/gi

Test: https://regexr.com/521r2

答案3

得分: 0

我认为需求不太清楚,但根据您的示例:

^[a-zA-ZÀ-ÖØ-öø-ÿ][ '_-a-zA-ZÀ-ÖØ-öø-ÿ]+$
^                        = 行的开头
$                        = 行的结尾
[a-zA-ZÀ-ÖØ-öø-ÿ]        = 匹配所有指定的字符,包括带有重音的字符
[ '_-a-zA-ZÀ-ÖØ-öø-ÿ]    = 与上面相同,但包括引号、空格和下划线
+                        = 一个或多个(贪婪匹配)

有关更多详细信息和示例,请参见[链接][1]。

最好。


  [1]: https://regex101.com/r/qJo95s/1
英文:

I think the requirements are not too clear but based on your examples:

^[a-zA-ZÀ-ÖØ-öø-ÿ][ '_-a-zA-ZÀ-ÖØ-öø-ÿ]+$


^                        = beginning of line
$                        = end of line
[a-zA-ZÀ-ÖØ-öø-ÿ]        = matches all these characters specified including one with accents
[ '_-a-zA-ZÀ-ÖØ-öø-ÿ]    = same as above except it includes the quote, blank space, underscore
+                        = one or more (greedy)

See this for more details and examples link

Best.

huangapple
  • 本文由 发表于 2020年4月8日 03:25:54
  • 转载请务必保留本文链接:https://java.coder-hub.com/61087887.html
匿名

发表评论

匿名网友

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

确定