1

I am attempting to extract a sub-string from a string after matching for 24 at the beginning of the string. The substring is a MAC id starting at position 6 till the end of the string. I am aware that a sub string method can do the job. I am curious to know a regex implementation.

String = 2410100:80:a3:bf:72:d45

After much trial and error, this the reg-ex I have which I think is convoluted.

[^24*$](?<=^\S{6}).*$

How can this reg-ex be modified to match for 24, then extract the substring from position 6 till the end of the line?

https://regex101.com/r/vcvfMx/2

Expected Results: 00:80:a3:bf:72:d45

41686d6564
  • 15,043
  • 11
  • 32
  • 63
RSSregex
  • 179
  • 6
  • 1
    `(?<=^24\S{3}).*$`? See it here: https://regex101.com/r/HqT0RV/1 – 41686d6564 Mar 31 '19 at 02:06
  • I got it. No worries. – RSSregex Mar 31 '19 at 02:21
  • `24` matches the first _**two**_ characters (which are "2" and "4", literally and respectively). `\S{3}` matches _**the next three**_ non-whitespace characters. So far, we've consumed _**five**_ characters so the next character is in _**position #6**_. See my answer below for additional info. – 41686d6564 Mar 31 '19 at 02:24

1 Answers1

2

You can use:

(?<=^24\S{3}).*$

Here's a demo: https://regex101.com/r/HqT0RV/1/

This will get you the result you expect (i.e., 00:80:a3:bf:72:d45). However, that doesn't seem to be a valid MAC address (the 5 at the end seems to be not part of the MAC). In which case, you should be using something like this:

(?<=^24\S{3})(?:[0-9a-f]{2}:){5}[0-9a-f]{2}

Demo: https://regex101.com/r/HqT0RV/2

Breakdown:

(?<=            # Start of a positive Lookbehind.
    ^           # Asserts position at the beginning of the string.
    24          # Matches `24` literally.
    \S{3}       # Matches any three non-whitespace characters.
)               # End of the Lookbehind (five characters so far).
(?:             # Start of a non-capturing group.
    [0-9a-f]    # A number between `0` and `9` or a letter between `a` and `f` (at pos. #6).
    {2}         # Matches the previous character class exactly two times.
    :           # Matches `:` literally.
)               # End of the non-capturing group.
{5}             # Matches the previous group exactly five times.
[0-9a-f]        # Any number between `0` and `9` or any letter between `a` and `f`.
{2}             # Matches the previous character class exactly two times.
41686d6564
  • 15,043
  • 11
  • 32
  • 63
  • You are right. It should be only up to `4`. That was a copy/paste error. – RSSregex Mar 31 '19 at 02:33
  • Ahmed, What is the purpose of the non-capturing group? I do not see multiple groups returned. I am trying to understand where in the string is `?:` applied. – RSSregex Mar 31 '19 at 15:19
  • @RSSregex _"I do not see multiple groups returned"_ That's because it's a **non-capturing** group. _"What is the purpose of the non-capturing group?"_ [This](https://stackoverflow.com/q/3512471/4934172) should answer your question; but in a few words, non-capturing groups are typically used when you need to _group_ part of the match but you don't want to _capture_ it separately. There are many reasons you might need to group a part of the match (e.g., repeat it, make it optional, alternation, etc.) In our case here, we need to repeat the group (which is done using `{5}`). – 41686d6564 Mar 31 '19 at 15:28