开发者

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

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"

I tried this

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

but then if y = 1-9 it does not match the expression.

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.

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.


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:

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!")


Try this:

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]). It works, but isn't really efficient because the alternatives can only be distinguished past the first character, i.e. this can require backtracking.

The regex ([1-3][0-9]?|4[0-8]?|[5-9]) matches the same strings. 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])


GigabitEthernet [1-6]/0/((4[0-8])|([1-3]?\d))

this handles the last part with 2 options. one for everything up to 39 and one for 40-48

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜