-1

I got help from you guys to extract the MAC address and UUID from textfiles using this regex pattern:

$Pattern = '([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2}),\s+(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})'

Could some friendly soul break down the pattern for me to help me understand how it works?

I then need to extract date and time as well that is written in format YYYY-MM-DD HH:MM:SS

1 Answers1

2

For the future, http://regexr.com/ is a great place to test Regex, and it will has a cheat-sheet on the left, and will explain things you highlight.

For this pattern

() = patttern group (orginization/grouping refrence)
[] = match anything in this character group
0-9/A-z = Match this digit/character range
{#} = match previous group # times
\s = match white space
\ = escape next character, use it as a literal or if the next character is a letter, match anything in that predefined character set.

So YYY-MM-DD HH:MM:SS in regex is (Never use regex to validate a date however, as there are too many exceptions to make it worth while; like Feb 28. Date validation requires a calendar API of some kind)

[0-9]{3}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}
Tezra
  • 7,096
  • 2
  • 19
  • 59
  • 2
    `75:98:12` is my favorite time of day :-) – Mathias R. Jessen Apr 20 '17 at 15:50
  • @MathiasR.Jessen Which is technically valid in an overflow kind of way. I would only use Regex to pick up a date/time. You should ALWAYS use a calendar API to validate it (because leap days, skipped leap days, DLS, leap seconds. with that many exceptions, Regex is a horrible validation tool.) – Tezra Apr 20 '17 at 16:34
  • Sorry dude, I just couldn't help myself ^_^ – Mathias R. Jessen Apr 20 '17 at 16:39
  • 1
    I did upvote you, and the validation point is important for people new to regex and think it can do everything =P – Tezra Apr 20 '17 at 16:41
  • This was hard to learn. I can match each part alone but not all at the same time. ([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2}),\s+(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})([0-9]{2}:[0-9]{2}:[0-9]{2})([0-9]{2}-[0-9]{2}-[0-9]{4}) – Markus Sacramento Apr 24 '17 at 08:20