Creating Regex grouping of "OR" conditions to shorten an expression

Viewed 34

I am capturing a string that is being passed in a text message. The condition is based on the word "contact".

Here are three sample texts:

ntpd process is not running on lnx31 contact: app-support. @monitoringautomation
ntpd process is not running on lnx31 contact: app-support, @monitoringautomation
ntpd process is not running on lnx31 contact app-support @monitoringautomation

My current regex is:

/(?i)contact:* (\S+),|(?i)contact:* (\S+)\.|(?i)contact:* (\S+)\s*/gm

My question, is there another way to clean this expression up or to shorten it? I have tried the following example, but it does not capture the app team when a period or comma is used, it includes it in the match.

/((?i)contact:* (\S+)(,|\.|\s*))/gm
1 Answers

You can use

(?i)\bcontact:*\s*([^,.\s]+)

See the regex demo.

Details:

  • (?i) - case insensitive inline modifier option
  • \b - a word boundary
  • contact - a string contact
  • :* - zero or more colons
  • \s* - zero or more whitespaces
  • ([^,.\s]+) - Group 1: one or more chars other than whitespace, comma and period.

See a Python demo:

import re
text = """ntpd process is not running on lnx31 contact: app-support. @monitoringautomation
ntpd process is not running on lnx31 contact: app-support, @monitoringautomation
ntpd process is not running on lnx31 contact app-support @monitoringautomation"""
print( re.findall(r"(?i)\bcontact:*\s*([^,.\s]+)", text) )
# => ['app-support', 'app-support', 'app-support']
Related