简体   繁体   English

正则表达式为“字符串x / 0 / y”

[英]Regex expression for “string x/0/y”

Hallo everyone, 大家好

I am trying to find a resolution for a regex expression: 我试图找到正则表达式的解决方案:

I want to have something like this 我希望有这样的东西

"string x/0/y" where x is in a range between 1-6 and y is in range between 1-48 “x / 0 / y”,其中x在1-6和y之间的范围内,在1-48的范围内

I tried this: 我试过这个:

interface GigabitEthernet [1-6]/0/([1-4]*[1-8])

but then if y = 50 it still takes 5 under consideration and drops "0" 但是如果y = 50,它仍然需要考虑5并且下降“0”

I tried this 我试过这个

interface GigabitEthernet [1-6]/0/([1-4][1-8])

but then if y = 1-9 it does not match the expression. 但是如果y = 1-9那么它与表达式不匹配。

I would appreciate any help on this. 我将不胜感激任何帮助。

Thank you ! 谢谢 !

Try ([1-9]|[1-3][0-9]|4[0-8]) for the second part of your regex. 试试([1-9]|[1-3][0-9]|4[0-8])你的正则表达式的第二部分。

Keep in mind that if you need to do lots of similar searches, regex alone isn't necessarily the best tool for the job. 请记住,如果您需要进行大量类似的搜索,单独使用正则表达式不一定是该工作的最佳工具。 Your program could instead search for the general pattern of /(\\d+)/0/(\\d+)/ , extract the match groups, then validate the numeric ranges. 您的程序可以搜索/(\\d+)/0/(\\d+)/的一般模式,提取匹配组,然后验证数值范围。

I don't recommend trying to do numeric range checking within a regular expression. 我不建议尝试在正则表达式中进行数值范围检查。 It's hard to write and even harder to read. 写作难,甚至难以阅读。 Instead, use a regular expression like this: 相反,使用这样的正则表达式:

(\d)/0/(\d{1,2})

Then, using the captured groups, check to make sure that the first one is 然后,使用捕获的组,检查以确保第一个是

x >= 1 and x <= 6

and the second one is 第二个是

y >= 1 and y <= 48

This will be much easier to read later, when you need to come back to it. 当您需要回到它时,这将更容易阅读。


A concrete example in Python might be: Python中的一个具体示例可能是:

s = "5/0/14"
m = re.match(r"(\d)/0/(\d{1,2})", s)
if m is not None:
    x = int(m.group(1))
    y = int(m.group(2))
    if x >= 1 and x <= 6 and y >= 1 and y >= 48 then
        print("Looks good!")

尝试这个:

interface GigabitEthernet [1-6]/0/([1-9]|[1-3][0-9]|4[0-8])

Regex is not very elegant when it comes to expressing such arbitrary number ranges. 在表达这样的任意数字范围时,正则表达式并不是很优雅。 Either of these should work though: 其中任何一个应该工作:

interface GigabitEthernet [1-6]/0/([1-3][0-9]|4[0-8]|[1-9])$

or 要么

interface GigabitEthernet [1-6]/0/([1-3][0-9]|4[0-8]|[1-9](?![0-9]))

One or the other might be more or less suited to your regex engine or target data, but only you would know that. 其中一个或多或少可能适合您的正则表达式引擎或目标数据,但只有您知道。

Others have suggested ([1-9]|[1-3][0-9]|4[0-8]) . 其他人建议([1-9]|[1-3][0-9]|4[0-8]) It works, but isn't really efficient because the alternatives can only be distinguished past the first character, ie this can require backtracking. 它起作用,但效率不高,因为替代品只能在第一个字符之后区分,即这可能需要回溯。

The regex ([1-3][0-9]?|4[0-8]?|[5-9]) matches the same strings. 正则表达式([1-3][0-9]?|4[0-8]?|[5-9])匹配相同的字符串。 However, here the first character uniquely determines which alternative is taken, and no backtracking is required. 但是,这里第一个字符唯一地确定采用哪种替代方案,并且不需要回溯。

You need something like this 你需要这样的东西

[1-6]/0/([1-9] | [1-3][0-9] | 4[0-8]) [1-6] / 0 /([1-9] | [1-3] [0-9] | 4 [0-8])

GigabitEthernet [1-6]/0/((4[0-8])|([1-3]?\\d)) 千兆以太网[1-6] / 0 /((4 [0-8])|([1-3]?\\ d))

this handles the last part with 2 options. 这处理最后一部分有2个选项。 one for everything up to 39 and one for 40-48 一个可以达到39个,一个可以达到40-48个

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

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