简体   繁体   English

不允许在字符串中的任何位置使用 '.'(dot)(正则表达式)

[英]Do not allow '.'(dot) anywhere in a string (regular expression)

I have a regular expression for allowing unicode chars in names(Spanish, Japanese etc), but I don't want to allow '.'(dot) anywhere in the string.我有一个正则表达式允许在名称中使用 unicode 字符(西班牙语、日语等),但我不想在字符串中的任何位置允许“.”(点)。

I have tried this regex but it fails when string length is less than 3. I am using xRegExp.我已经尝试过这个正则表达式,但是当字符串长度小于 3 时它会失败。我正在使用 xRegExp。

^[^.][\\pL,.'-''][^.]+$

For Example:例如:

NOËL             // true
Sanket ketkar    // true
.sank            // false
san. ket         // false
NOËL.some        // false

Basically it should return false when name has '.'基本上,当名称具有“。”时,它应该返回 false。 in it.在里面。

Your pattern ^[^.][\\pL,.'-''][^.]+$ matches at least 3 characters because you use 3 characters classes, where the first 2 expect to match at least 1 character and the last one matches 1 or more times.您的模式^[^.][\\pL,.'-''][^.]+$至少匹配 3 个字符,因为您使用 3 个字符类,其中前 2 个期望匹配至少 1 个字符,最后一个一个匹配1次或多次。

You could remove the dot from your character class and repeat that character class only to match 1+ times any of the listed to also match when there are less than 3 characters.您可以从字符 class 中删除点,然后重复该字符 class 仅匹配列出的任何一个以上的 1 倍以在少于 3 个字符时也匹配。

^[\p{L} ,'‘’-]+$

Regex demo正则表达式演示


Or you could use a negated character class :或者您可以使用否定字符 class

^[^.\r\n]+$
  • ^ Start of string ^字符串开头
  • [^.\r\n]+ Negated character class, match any char except a dot or newline [^.\r\n]+否定字符 class,匹配除点或换行符以外的任何字符
  • $ End of string $字符串结尾

Regex demo正则表达式演示

You could try:你可以试试:

^[\p{L},\-\s‘’]+(?!\.)$

As seen here: https://regex101.com/r/ireqbW/5如此处所示: https://regex101.com/r/ireqbW/5

Explanation -解释 -

The first part of the regex [\p{L},\-\s'']+ matches any unicode letter, hyphen or space (given by \s )正则表达式的第一部分[\p{L},\-\s'']+匹配任何 unicode 字母、连字符或空格(由\s给出)

(?.\.) is a Negative LookAhead in regex, which basically tells the regex that for each match, it should not be followed by a . (?.\.)是正则表达式中的 Negative LookAhead ,它基本上告诉正则表达式对于每场比赛,它后面不应该跟一个.

^[^.]+$

It will match any non-empty string that does not contain a dot between the start and the end of the string.它将匹配任何在字符串的开头和结尾之间不包含点的非空字符串。

If there is a dot somewhere between start to end (ie anywhere) it will fail.如果在开始到结束(即任何地方)之间有一个点,它将失败。

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

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