开发者

Common regex to match patterns Gig1/2 and Gig1/2/3

What is the common regex to match below patterns?

  • Gig1/2
  • Gig1/2/3

I am having the below pattern to match the strings Gig1/1 and Gig1/1/1. But the issue is,Gig1/1/1 is matching with both patterns.Please let me know ,how t开发者_高级运维o avoid this?

Pat1 : ".*?(\d+)/(\d+)"

Pat2 : ".*?(\d+)/(\d+)/(\d+)"


Pat1: ^\p{L}+(\d+)/(\d+)$

Pat2: ^\p{L}+(\d+)/(\d+)/(\d+)$

If you just apply ^.*?(\d+)/(\d+)$ to Gig1/2/3, the .*? part will match Gig1/ since the dot can match any character (no difference whether it's lazy or not).

You need to be more specific about what may come before the numbers. I'm assuming letters, so I used \p{L} which means "any (Unicode) letter".

Don't forget to double the backslashes before pasting those regexes in a Java string.


You need to add start and end anchors to your regex:

Pat1 : ^"\w+(\d+)/(\d+)"$

Pat2 : ^"\w+(\d+)/(\d+)/(\d+)"$

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜