I need to extract IDs from a string : 1;#ChapitreA;#2;#ChapitreB;
Here, IDs are 1 and 2
I tried (^|;#)([0-9]?)(;) but it returns too many results => Here
Or behind :
0-2 1;
0-0 null
0-1 1
1-2 ;
12-16 ;#2;
12-14 ;#
14-15 2
15-16 ;
Constraint : I cannot use groups as this RegEx will be use in Nintex Workflow which doesn't support it. By the way I can't use any programming language neither to manage the result.. I need the RegEx to return the exact result I need (IDs) and nothing more.
Solution : (?:(?<=^)|(?<=;#))\d+(?=;)
Using groups : (?:^|;#)(\d+);
Without using groups : (?:(?<=^)|(?<=;#))\d+(?=;)
Thx #wiktor-stribiżew
Related
I have a small issue, I am trying to get specific characters from a long string using regex but I am having trouble.
Workflow
Prometheus --> Grafana --> Variable (using regex)
I can't use anything other than Regex expressions to achieve this result
I am currently using this expression to grab the long string from some json output:
.*channel_id="(.*?)".*
FROM THIS
{account_id="XXXXXXX-xxxx-xxxx-xxxx-xxxxxxxxxx",account_name="testalpha",channel_id="s0022110430col0901241usa",channel_abbr="s0022109430col}
This returns a string that's ALWAYS 24 characters long:
s0022110430col0901241usa
PROBLEM:
I need to grab the 3 letters 'col' and 'usa' as they are the two teams that are playing, ideally I would be able to pipe the results from the first regex to get these values (the position is key, since the first value will ALWAYS be the 12-14th characters and the second value is the last 3 characters) if I could output these values in uppercase with the string "vs" in between to create a string such as:
COL vs USA
or
ARG vs BRA
I am open to any and every suggestion anyone may have
Thank you!
PS - The uppercase thing is 'nice to have' BUT not needed
I'm still learning RegEx, so this is all I could come up with:
For the col (first team):
(?<=(channel_id=".{11}))\w{3}
For the usa (second team):
(?<=(channel_id=".{21}))\w{3}
Can you define the channel_id?
It begins with 's' and then there are many numbers. If they are always numbers, you can use this regex:
channel_id=".[0-9]+([a-z]+)[0-9]+([a-z]+)
You will get 2 groups, one with "col" and the other with "usa".
Edit:
Or if you just know, that you have always the same size, you can use something like:
channel_id=".{11}([a-z]+).{7}([a-z]+)
I have the following line :
3EAM7A 1 3 EI AMANDINE MRV SHP 70 W 0 SH3-A1 1 SHP 70W OVOIDE AI E27 SON PIA PLUS
I'd like to get the string : EI AMANDINE MRV SHP 70 W. So I decided to select the strings between 1 (can also be 2, 3 or 99) and 0 (can also be 1, 2, 3, 4 or 5).
I tried :
(0|1|2|3|99)(.*)(0|1|2|3|4|5)
But I have this result :
EAM7A 1 3 EI AMANDINE MRV SHP 70 W 0 SH3-A1 1 SHP 70W OVOIDE AI E
that is not what I want to obtain.
Do you have an idea in regex to make that selection work ?
Thanks !
You were pretty close! Try this:
\b(?:0|1|2|3|99) ([^0|1|2|3|99].*?) (?:0|1|2|3|4|5)\b
Regex101
I think that you want to match "word" 4 to 9?
Your desired match will be in group 1
^(\S+\s){3}((\S+\s){6})
Enable the multiline option if you have a whole file of subject strings.
You can try with:
\s(?:[0-3]|99)\s([A-Z].*?)\b(?:[0-5])\b
DEMO
and get string by group $1. Or if your language support look around, try:
(?<=\s[0-3]\s|99)[A-Z].+?(?=\s[0-5]\s)
DEMO
to get match directly.
Another solution that is based on matching all initial space + digit sequences:
\b(?:(?:[0-3]|99)\b\s*)+(.*?)\s*\b(?:[0-5])\b
See demo
The result is in Group 1.
With \b(?:(?:[0-3]|99)\b\s*)+ the rightmost number from the allowed leading set is picked.
You can use following regex :
(?:(?:[0-3]|99)\s)+(.*?)\s(?:[0-5])\s
See demo https://regex101.com/r/iX6oE1/6
Also note that for matching a range of number you can use a character class instead of multiple OR.
I want to match a 3-digit number only from a webpage.
So for example if webpage has number 1 599-+ (white space between 1 and 5 and -+ signs after). I only want to capture/match numbers between 0 and 599-+ and nothing else.
My regex is: regex(?:^|(?:[^\d\s]\s*))([0-5]\d\d-+) but this one also matches "i 1599-+"
or regex(\^[0-5]?[0-9]?[0-9]-+$) doesnt work either...
A solution would be to use this regular expression with a non capturing group matching either the start of the string or something that's not a digit (with a little more verbosity due to space handling) :
(?:^|(?:[^\d\s]\s*))([0-5]\d\d)
Examples (in javascript as you didn't specify a language) :
"1 599".match(/(?:^|(?:[^\d\s]\s*))([0-5]\d\d)/) => null
"a sentence with 1 599 inside".match(/(?:^|(?:[^\d\s]\s*))([0-5]\d\d)/) => null
"another with 599".match(/(?:^|(?:[^\d\s]\s*))([0-5]\d\d)/) => ["h 599", "599"]
"599 at the start".match(/(?:^|(?:[^\d\s]\s*))([0-5]\d\d)/) => ["599", "599"]
(desired group is at index 1)
I hope this is needed for you.Try it, if it is not fulfilling.Write a little more description.
/^[0-5]?[0-9]?[0-9]$/.test("599");
From the above I understood and developed this, I think this is what you needed.
/^[0-5]?[0-9]?[0-9][\+|-]?$/.test("599");
In the above regex I made + - as optional and it'll check for presence of any one sign.
If you want in the order of -+ then try this
/^[0-5]?[0-9]?[0-9][\-][\+]$/.test("99-+"); .
Okay #user3214294
I'm looking for a expression range for monetary purposes. It needs to be 1 - 1 million and allow commas and periods. I don't need a min/max of (, and .) for correct formatting but I would like the digits after a period to be a min/max of 2 for actual cent values. Thanks
In Range:
640 or 5,000.35 or 999,000
Not in Range:
01 or 1,000,000.01 or 333,567.678
What I would suggest is :
Use something like that to verify that the input has a specific format :
(here's a demo - http://regexr.com?30l28)
(1[\.,])?([0-9]{1,3}[\.,])?([0-9]{1,3})([\.,][0-9]{1,2})
And then test the value range :
is value<1.000.000?
My regex is by no means 100% complete, but it DOES verify your general number format though.
This should do it:
^(1(\.\d{2})?|[1-9]\d{0,2}(,?\d{3})?(\.\d{2})?)|1((,000){0,2}|(000){0,2})(\.00)?$
But it would probably easier if you normalize the value first (e. g. remove any character except digits and the .) and then parse it.
I spent lot time figuring out a simple regex to return a group (only 1st group).
So the string can be -
"No purchase required" or "Purchase of $50.00 worth groceries is required."
I am trying to write a regex which can parse "No" or "50" based on the given string.
This is what I have written.
(?:(No) monthly maintenance|Purchase of \$([\d\.]+ worth groceries)
This works fine but I want my output as 1st group/group 1 only.
Why not just use /(?:No monthly maintenance|Purchase of $([0-9.]+) worth groceries)/.
The match will fail if it's not in one of those formats, and Group 1 matches '' for the "No monthly maintenance" case, or the number for other case.
If you really need to capture the string No or the number, you might need to get a little more complicated and do something like:
/(?:Purchase of $)?([0-9.]+|No) (?:monthly maintenance|worth groceries)/
Most languages count the matching groups in order, regardless of whether they are in a non-capturing group ((?:...|...)), so forcing the interesting part into the first capturing group might be more trouble than it's worth.
Depending on what language you are using you might want to try using two different precompiled regular expressions and return the matching group of the first match, that way you can easily fit the interesting part into the first group.
I'm not sure you can get the result in group number 1, but you can get both results to appear in the same named group. Here's an example in PowerShell:
$input1 = 'Purchase of $50.00 worth groceries is required'
$input2 = 'No monthly maintenance required'
$re = '(?:(?<xyz>No) monthly maintenance|Purchase of \$(?<xyz>[\d\.]+) worth groceries)'
$match = [regex]::Match($input1, $re)
$match.Groups['xyz']
$match = [regex]::Match($input2, $re)
$match.Groups['xyz']
Which results in the following:
Success : True
Captures : {50.00}
Index : 13
Length : 5
Value : 50.00
Success : True
Captures : {No}
Index : 0
Length : 2
Value : No
Not all languages support named groups though. Since PowerShell runs on the .NET Framework, this will work for any .NET language.