Regex pattern for Prometheus exporter - regex

I am trying to create a regex pattern for one of the prometheus exporter (jmx exporter) configuration file to export weblogic jms queues.
My String is as below
(com.bea<ServerRuntime=AC_Server-10-100-40-122, Name=iLoyalJMSModule!AC_JMSServer#AC_Server-10-100-40-122#com.ibsplc.iloyal.eai.EN.retro.outErrorqueue, Type=JMSDestinationRuntime, JMSServerRuntime=AC_JMSServer#AC_Server-10-100-40-122><>MessagesCurrentCount)
And the RegEx is as below
Pattern
com.bea<ServerRuntime=(.+), Name=(.+), Type=(.+), JMSServerRuntime=(.+)<>(MessagesCurrentCount|MessagesPendingCount)
Name to display in Prometheus exporter output
name: "weblogic_jmsserver_$1_$5"
Current Output
weblogic_jmsserver_ac_server_10_100_40_122_messagescurrentcount
Now i would like to add the queue outErrorqueue name to my output from the Name= string and the final output should be like below.
Required Output
weblogic_jmsserver_ac_server_10_100_40_122_outErrorqueue_messagespendingcount

You could change the number of capture groups from 5 to the 2 that you need in the replacement. Instead of using .+, you can either use .*? or use a negated character class to match any char except a commen [^,]+
If the surrounding parenthesis of the example data should not be part of the replacement, you can use:
\(com\.bea<ServerRuntime=([^,]+), Name=[^,]+, Type=[^,]+, JMSServerRuntime=.+?<>(Messages(?:Current|Pending)Count)\)
In the replacement use:
weblogic_jmsserver_$1_outErrorqueue_$2
See a regex demo

Related

Regex match zero or one group

I have filenames in format <pod-name>_<namespace-name>_<container-name>-<dockerid>.log
For example:
pod-name_namespace-name_container-name-7a1d0ed5675bdb365228d43f470fcee20af5c8bea84dd6d886b9bf837a9d358c.log
pod-name_namespace-name-1234567890_container-name-7a1d0ed5675bdb365228d43f470fcee20af5c8bea84dd6d886b9bf837a9d358c.log
Actually this is the k8s container's log files.
The namespace-name may contain numeric postfix that represents automation system run id (github.run_id - 10 digits number).
I need to parse filenames with regex to extract pod name, namespace name without run id, run id, container name and docker id.
Regex based on default fluentbit kubernetes parser that I need to change for our usage:
(?<pod_name>[a-z0-9](?:[-a-z0-9]*[a-z0-9])?(?:\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*)_(?<namespace_name>[^_]+)(-(?<run_id>\d{10,}))_(?<container_name>.+)-(?<docker_id>[a-z0-9]{64})\.log$
https://rubular.com/r/CROBxpHHgX5UZx
The regex above parses well filenames that contains namespace with run id, but fails to parse namespace without run id:
pod-name_namespace-name_container-name-7a1d0ed5675bdb365228d43f470fcee20af5c8bea84dd6d886b9bf837a9d358c.log
https://rubular.com/r/6MSQsnuGzrkVJG
In this case the run_id should be empty string
How to fix it that it match both cases?
You can use
(?<pod_name>[a-z0-9](?:[-a-z0-9]*[a-z0-9])?(?:\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*)_(?<namespace_name>[^_]+?)(-(?<run_id>\d{10,}))?_(?<container_name>.+)-(?<docker_id>[a-z0-9]{64})\.log$
See the regex demo.
The main point is to make two changes in (?<namespace_name>[^_]+)(-(?<run_id>\d{10,})) part:
make the [^_]+ pattern lazy, so that it could match as few chars other than _ as possibe, i.e. add a ? after +
make the (-(?<run_id>\d{10,})) part optional by adding a ? quantifier after the group.

How to write Regex expression to extract the content in brackets, after string and the first match?

I would like to use Regular expression to extract content between brackets, after some specific string and the 1st match.
Example text:
**-n --command PING being applied--:
Wed May 34 7:23:18 2010
[ZZZ_6323] Command [ping] failed with error [[TEZZZGH_IUE] [[EIJERTMMMMIJE_EIEJ] gdyugedyue Service [ABC] is not available in domain [DEF]. Check the content and review diejidjei. Service [ABC] Domain [DEF] ] did not ping back. It might be due to one of the following reasons:
=> Reason1
=> Reason3
=> Reason 4: deijdije djkeoidjeio.
info=4343 day=Mon year=2010*
I would like to extract the string between [] but after string Service and 1st match as Service could appear again later. In this case ABC
Could someone help me?
I am not able to combine these three conditionals.
Thanks
Assuming that you don't care about capturing square brackets inside the [ ] pair, by far the easiest way to do this is to use the following simple regex:
Service (\[[^\]]*\])
and extract only the 1st capturing group from the result using whatever regex functionality you're using. For example, using JS, you would write
string.match(/Service (\[[^\]]*\])/)[1]
to extract the first capturing group.
If you instead want a regex that will only capture the first occurrence, you can exploit the greedy nature of the * quantifier and change the regex to this:
Service (\[[^\]]*\]).*
Service \[([^\]]+)\]
will match Service [anything besides brackets] and capture anything besides brackets in group number 1. Since regex engines work left-to-right, the first match will be the leftmost match.
Test it live on regex101.com.
In PHP, you could do this (code snippet generated by RegexBuddy):
if (preg_match('/Service \[([^\]]+)\]/', $subject, $groups)) {
$result = $groups[1];
} else {
$result = "";
}
The definition of the group name How should I write it? I know that it can be like this: (?) but I dont know how to combine it with this part Service [([^]]+)] in a single way

Regex: select the XML messages and time stamp from the log

I am going to streaming the logs in to nxlog, i need to push xml messages in to nexlog server, To select the XML message:
(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d{3})(.*)(my sentence 1....|my sentence 2 : [\S+\s+]*>\n)(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d{3})
But I am not able to select all XML messages from logs
https://regex101.com/r/iA8qE5/5
In your regex you have to close the alternation using ) after:
(Message Picked from the queue....|Response Message :
Using a + inside the character class would have a different meaning and would match a plus sign literally. The plus is greedy so you have to make it non greedy using a question mark to let [\S\s]+ not match all lines.
Update [\S+\s+]*>\n)
to
)([\S\s]+?>)\n
Your match is in the 4th capturing group.
(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d{3})(.*)(Message Picked from the queue....|Response Message : )([\S\s]+?>)\n(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d{3})
Regex demo
Not that if you don't need all the capturing groups, you can also omit them and take only the first capturing group (Demo)
it capture date from starting line, message and xml. it using gms flag, Demo
^([\d-\.\s\:]+)\s.*?-\s([\w\s:\.]+)(<\w+.*?)\n\d{4}
date and xml only
^([\d-\.\s\:]+)\s.*?(<\w+.*?)\n\d{4}

Regex processing in systemverilog using svlib

I am a new user of svlib package in systemverilog environment. Refer to Verilab svlib. I have following sample text , {'PARAMATER': 'lollg_1', 'SPEC_ID': '1G3HSB_1'} and I want to use regex to extract 1G3HSB from this text.
For this reason, I am using the following code snippet but I am getting the whole line instead of only the information.
wordsRe = regex_match(words[i], "\'SPEC_ID\': \'(.*?)\'");
$display("This is the output of Regex: %s", wordsRe.getStrContents())
Can anybody direct me what is going wrong?
The output I am getting : {'PARAMATER': 'lollg_1', 'SPEC_ID': '1G3HSB_1'}
And, I want to get: 1G3HSB_1
It seems you need to get the contents of the first capturing group with getMatchString(1). Also, you need to use a greedy quantifier (lazy ones are not POSIX compliant) and a negated bracket expression - [^']* instead of .*?:
wordsRe = regex_match(words[i], "\'SPEC_ID\': \'([^\']*)\'");
$display("This is the output of Regex: %s", wordsRe.getMatchString(1))
See the User Guide details:
getMatchString(m) is always exactly equivalent to calling the range method on the Str object containing the string that was searched:
range(getMatchStart(m), getMatchLength(m))

Can I capture a label not found in the test string using regex?

Assuming I have some strings of the following type:
session opened by (uid=0)
session opened by scotty
Is it possible to write a regex that will either capture the text "root" if (uid=0) is found in the string, otherwise capture the normal user name (i.e. scotty)?
Regex does not allow you to capture anything that is missing from the input string. If you know the structure of the input text, you can have a regex pattern return the required part. Here is an example that works for .NET-based regex flavor:
(?s)(?<=\(uid=0\).*opened by )\w+
Matches Found:
[0][0] = scotty