Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pattern to match everything except a string of 5 digits

I only have access to a function that can match a pattern and replace it with some text:

Syntax
regexReplace('text', 'pattern', 'new text'

And I need to return only the 5 digit string from text in the following format:

CRITICAL - 192.111.6.4: rta nan, lost 100%
Created Time    Tue, 5 Jul 8:45
Integration Name    CheckMK Integration
Node    192.111.6.4
Metric Name POS1
Metric Value    DOWN
Resource    54871
Alert Tags  54871, POS1

So from this text, I want to replace everything with "" except the "54871".

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have come up with the following:

regexReplace("{{ticket.description}}", "\w*[^\d\W]\w*", "")

Which almost works but it doesn’t match the symbols. How can I change this to match any word that includes a letter or symbol, essentially.

enter image description here

As you can see, the pattern I have is very close, I just need to include special characters and letters, whereas currently it is only letters:

enter image description here

>Solution :

You can match the whole string but capture the 5-digit number into a capturing group and replace with the backreference to the captured group:

regexReplace("{{ticket.description}}", "^(?:[\w\W]*\s)?(\d{5})(?:\s[\w\W]*)?$", "$1")

See the regex demo.

Details:

  • ^ – start of string
  • (?:[\w\W]*\s)? – an optional substring of any zero or more chars as many as possible and then a whitespace char
  • (\d{5}) – Group 1 ($1 contains the text captured by this group pattern): five digits
  • (?:\s[\w\W]*)? – an optional substring of a whitespace char and then any zero or more chars as many as possible.
  • $ – end of string.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading