简体   繁体   English

正则表达式只允许 1 个破折号

[英]regular expression to allow only 1 dash

I have a textbox where I get the last name of a user.我有一个文本框,可以在其中获取用户的姓氏。 How do I allow only one dash (-) in a regular expression?如何在正则表达式中只允许一个破折号 (-)? And it's not supposed to be in the beginning or at the end of the string.它不应该在字符串的开头或结尾。
I have this code:我有这个代码:

Pattern p = Pattern.compile("[^a-z-']", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(name);

Try to rephrase the question in more regexy terms.尝试用更正则化的术语重新表述这个问题。 Rather than "allow only one dash, and it can't be at the beginning" you could say, "the string's beginning, followed by at least one non-dash, followed by one dash, followed by at least one non-dash, followed by the string's end."而不是“只允许一个破折号,它不能在开头”,您可以说,“字符串的开头,后跟至少一个非破折号,接着是一个破折号,然后是至少一个非破折号,紧接着是字符串的结尾。”

  • the string's beginning: `^字符串的开头:`^
  • at least one non-dash: [^-]+至少一个非破折号: [^-]+
  • followed by one dash: -后跟一个破折号: -
  • followed by at least one non-dash: [^-]+后跟至少一个非破折号: [^-]+
  • followed by the string's end: $后跟字符串的结尾: $

Put those all together, and there you go.将所有这些放在一起,然后就可以了。 If you're using this in a context that matches against the complete string (not just any substring within it), you don't need the anchors -- though it may be good to put them in anyway, in case you later use that regex in a substring-matching context and forget to add them back in.如果您在与完整字符串(不仅仅是其中的任何子字符串)匹配的上下文中使用它,则不需要锚点——尽管将它们放在任何地方可能会很好,以防您以后使用它在子字符串匹配上下文中使用正则表达式,而忘记将它们添加回来。

Why not just use indexOf() in String ?为什么不直接在String使用indexOf()

String s = "last-name";
int first = s.indexOf('-');
int last = s.lastIndexOf('-');

if(first == 0 || last == s.length()-1) // Checks if a dash is at the beginning or end
    System.out.println("BAD");
if(first != last) // Checks if there is more than one dash
    System.out.println("BAD");

It is slower than using regex but with usually small size of last names it should not be noticeable in the least bit.它比使用正则表达式慢,但通常姓氏很小,至少应该不明显。 Also, it will make debugging and future maintenance MUCH easier.此外,它将使调试和未来维护更加容易。

This regex represents one or more non-hyphens , followed by a single hyphen, followed by one or more non-hyphens .此正则表达式表示一个或多个non-hyphens ,后跟单个连字符,后跟一个或多个non-hyphens

^[^\-]+\-[^\-]+$

I'm not sure if the hyphen in the middle needs to be escaped with a backslash... That probably depends on what platform you're using for regex.我不确定中间的连字符是否需要用反斜杠转义......这可能取决于您使用的正则表达式平台。

It looks like your regex represents a fragment of an invalid value, and you're presumably using Matcher.find() to find if any part of your value matches that regex.看起来您的正则表达式代表无效值的片段,并且您可能正在使用Matcher.find()来查找您的值的任何部分是否与该正则表达式匹配。 Is that correct?那是对的吗? If so, you can change your pattern to:如果是这样,您可以将模式更改为:

Pattern p = Pattern.compile("[^a-zA-Z'-]|-.*-|^-|-$");

which will match a non-letter-non-hyphen-non-apostrophe character, or a sequence of characters that both starts and ends with hyphens (thereby detecting a value that contains two hyphens), or a leading hyphen, or a trailing hyphen.这将匹配非字母非连字符非撇号字符,以连字符开头和结尾的字符序列(从而检测包含两个连字符的值),前导连字符尾随连字符。

Try pattern something like [az] -[az] .尝试像 [az] -[az] 这样的模式

Pattern p = Pattern.compile("[az] -[az] "); Pattern p = Pattern.compile("[az] -[az] ");

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

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