2

I'm need help figuring out a RegEx statement to help me match properly interfaces on a switch/router.

Example: Correct matches may be:

Fa1/0, Gig2/0/2, Fa3/0/33/333, G1/0/0:1, Gi0/0.900 etc.

The closest I've been able to get is:

[fget][a-z]*[0-9]+(?:[\/\.:][0-9]+)+

The problem is that is also matches the Fa1/0 part of Fa1/0/ or Fa1/0a which is incorrect and shouldn't be matched at all.

Can anyone assist me?

Given the string: Start Fa1/0/1 Gig2/0 Fa1/0/ Ending
Match ONLY: Fa1/0/1 and Gi2/0
Restriction: You cannot use: \s or ' ' (the parser I'm using doesn't allow it)
Modifier: Case is set to insensitive.

ADDITIONAL INFO/EDIT BELOW

This is actually for a RegEx parser that is in the new SecureCRT 7.3 terminal application.

The goal is to colorize proper syntax in real time, while removing colorization from improper syntax.

That is why we can't depend on the ^ and $ tokens, because you can never be assured the word will be at the start or ending of the line.

Properly matched whole words will follow the format of: Interface-Type + Number + Sub-interface (optional).

Interface-Type: Could be anything from 'F' to FastEthernet. Other prefixes include 'g' to Gigabit, 'e' to Ethernet, and 't' to ten.

Number: This will follow the pattern of and then (repeating). Example: 1/2 or 1/2/3 or even 1/2/3/4/5/6/7/8/10/12 would technically be valid for this case.

Sub-interface (Optional): Would follow the format of or . IE: .900 or :123

So, more string examples could be: F1/0/1.900 Gig2/0 Te1/0/0/1:23 The words could appear in any order and mixed many other words. IE: "Show Int F1/0/0" or "int Range fa1/0/1 - 24"

Miller
  • 34,962
  • 4
  • 39
  • 60
James
  • 21
  • 4
  • 1
    What is the language/tool you use? Give more input strings example. – Casimir et Hippolyte Sep 06 '14 at 01:41
  • You can describe the next possible character and put what you are interested by in a capture group: `(\b[efgt][a-z]*[0-9]+(?:[/.:][0-9]+)+)(?: |$)` – Casimir et Hippolyte Sep 06 '14 at 02:28
  • Unfortunately, the above code won't work because not only \s not allowed ' ' (a space) isn't allowed either. – James Sep 06 '14 at 02:37
  • Since SecureCRT is a proprietary tool, do you know if there is a reference documentation that describes the regex features available for the terminal? – Casimir et Hippolyte Sep 06 '14 at 02:39
  • Thanks. I've updated the original question above and deleted my poorly formatted replies. .. No, there's no useful reference documentation yet (RegEx support is beta). I often use regex101.com to test RegEx code first. So far, it seems to follow inline with that. Not supporting ' ' or \s has been the biggest pain so far. – James Sep 06 '14 at 02:51
  • 4
    did you try to put a space in a character class: `[ ]` or to escape it: `\ ` or to represent it with its hex code: `\x20`? Note that you can use `[^a-z0-9/:.]` in place of a space in the present context. – Casimir et Hippolyte Sep 06 '14 at 02:59
  • 2
    You are wonderful. Hitting the space bar at all isn't allowed, but the \x20 did the trick! I've been pounding my head for 2 days on this. Thank you so very much. – James Sep 06 '14 at 03:04

1 Answers1

0

This matches all the expressions you listed. Apart from a couple of minor changes, the main thing is to add a negative lookahead expression at the end.

[FG][a-z]*[0-9](?:[\/\.\:][0-9]+)*(?!\/)
Mark
  • 597
  • 5
  • 8