正则表达式,用于传递URL和失败的CIDR掩码

I have strings that can be URLs or CIDR-blocks. I want to determine whether a string is the path of a URL e.g.:

/9this can be/a/path123
/test
/r

or a CIDR-mask (/0 to /32):

/24
/12
/32

I want the regex to exclude CIDR-masks from the matches.

This is what I have so far:

^[/?]((?!([0-9]|[1-2][0-9]|3[0-2]))(?=[\S]))[\S ]*$

Which works, except for the case where the URL-path starts with a number:

/23example

I'm coding in Go, if that matters.

Maybe this will be adequate:

^[\/?].*[a-zA-Z].*$

See https://regex101.com/r/dw6Dtw/1

All it does is ensure that there's a letter in the path somewhere

I doubt that you might want to have a complex expression to pass those, maybe just adding a quantifier with 2 plus alpha would pass that. Maybe, an expression similar to this would suffice:

(.+?[a-z]{2,}[0-9]?.+)

enter image description here

DEMO

RegEx

If this expression wasn't desired or my assumptions were incorrect, it can be modified or changed in regex101.com.

RegEx Circuit

jex.im also helps to visualize the expressions.

enter image description here