简体   繁体   English

[a-zA-Z0-9 \ - ]的正则表达式允许使用破折号,但不允许在开头或结尾处

[英]Regex for [a-zA-Z0-9\-] with dashes allowed in between but not at the start or end

Update: 更新:

This question was an epic failure, but here's the working solution. 这个问题是史诗般的失败,但这是一个有效的解决方案。 It's based on Gumbo's answer (Gumbo's was close to working so I chose it as the accepted answer): 这是基于Gumbo的答案(Gumbo接近工作,所以我选择它作为接受的答案):

Solution: 解:

r'(?=[a-zA-Z0-9\-]{4,25}$)^[a-zA-Z0-9]+(\-[a-zA-Z0-9]+)*$'

Original Question (albeit, after 3 edits) 原始问题(虽然经过3次编辑)

I'm using Python and I'm not trying to extract the value , but rather test to make sure it fits the pattern. 我正在使用Python,我不是试图提取值 ,而是测试以确保它符合模式。

allowed values: 允许值:

spam123-spam-eggs-eggs1
spam123-eggs123
spam
1234
eggs123

Not allowed values: 不允许的值:

eggs1-
-spam123
spam--spam

I just can't have a dash at the starting or the end. 我只是不能在开始或结束时冲刺。 There is a question on here that works in the opposite direction by getting the string value after the fact, but I simply need to test for the value so that I can disallow it. 这里有一个问题是通过在事实之后获取字符串值而在相反方向工作,但我只需要测试该值以便我可以禁止它。 Also, it can be a maximum of 25 chars long , but a minimum of 4 chars long . 此外,它最多可以有25个字符长 ,但至少有4个字符长 Also, no 2 dashes can touch each other . 此外, 没有2个破折号可以互相接触

Here's what I've come up with after some experimentation with lookbehind, etc: 这是我在进行一些后观实验后得出的结果:

# Nothing here

Try this regular expression: 试试这个正则表达式:

^[a-zA-Z0-9]+(-[a-zA-Z0-9]+)*$

This regular expression does only allow hyphens to separate sequences of one or more characters of [a-zA-Z0-9] . 此正则表达式仅允许连字符分隔[a-zA-Z0-9]的一个或多个字符的序列。


Edit Following up your comment: The expression (…)* allows the part inside the group to be repeated zero or more times. 编辑跟进您的评论:表达式(…)*允许组内的部分重复零次或多次。 That means 这意味着

a(bc)*

is the same as 是相同的

a|abc|abcbc|abcbcbc|abcbcbcbc|…

Edit Now that you changed the requirements: As you probably don't want to restrict each hyphen separated part of the words in its length, you will need a look-ahead assertion to take the length into account: 编辑现在您已经更改了要求:由于您可能不希望限制其长度中每个连字符分隔部分的单词,因此您需要一个前瞻性断言来考虑长度:

(?=[a-zA-Z0-9-]{4,25}$)^[a-zA-Z0-9]+(-[a-zA-Z0-9]+)*$

The current regex is simple and fairly readable. 目前的正则表达式简单易读。 Rather than making it long and complicated, have you considered applying the other constraints with normal Python string processing tools? 您是否考虑过使用普通的Python字符串处理工具来应用其他约束,而不是让它变得冗长和复杂?

import re

def fits_pattern(string):
    if (4 <= len(string) <= 25 and
        "--" not in string and
        not string.startswith("-") and
        not string.endswith("-")):

        return re.match(r"[a-zA-Z0-9\-]", string)
    else:
        return None

It should be something like this: 它应该是这样的:

^[a-zA-Z0-9]+(-[a-zA-Z0-9]+)*$

You are telling it to look for only one char, either az, AZ, 0-9 or -, that is what [] does. 你告诉它只查找一个字符,az,AZ,0-9或 - ,这就是[]的作用。

So if you do [abc] you will match only "a", or "b" or "c". 因此,如果您执行[abc]您将只匹配“a”,或“b”或“c”。 not "abc" 不是“abc”

Have fun. 玩得开心。

If you simply don't want a dash at the end and beginning, try ^[^-].*?[^-]$ 如果您只是想在结尾处开始使用破折号,请尝试^[^-].*?[^-]$

Edit: Bah, you keep changing it. 编辑:呸,你不断改变它。

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

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