K20GH
K20GH

Reputation: 6271

Regex to match variable length, spaces and special chars?

I've got some strings like so

2020-03-05 11:23:25: zone 10 type Interior name 'Study PIR'
2020-03-05 11:57:15: zone 13 type Entry/Exit 1 name 'Front Door'

I've got the below regex that works for the first string, however I'm not sure how to get the product group to match the full group "Entry/Exit 1" The number can range from 1 - 100

(?<Date>[0-9]{4}-[0-2][1-9]-[0-2][1-9]) (?<Time>2[0-3]|[01][0-9]:[0-5][0-9]:[0-5][0-9]): (?<msgType>\w+) (?<id>[0-9]+) (?<type>\w+) (?<product>\w+) \w+ (?<deviceName>'([^']*)')

Any ideas how I can modify this to match?

Upvotes: 1

Views: 516

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626893

Your product group pattern should be

(?<product>\w+(?:\/\w+\s+\d+)?)

See the regex demo

Details

  • \w+ - 1+ word chars
  • (?:\/\w+\s+\d+)? - an optional sequence of
    • \/ - a / char
    • \w+ - 1+ word chars
    • \s+ - 1+ whitespaces
    • \d+ - 1+ digits.

If the format is unknown, or does not fit the above description, just use (?<product>.*?), see demo.

Upvotes: 1

Related Questions