Regex to parse out substring, two options - regex

I have a list of group names that all either contain prod or nonprod inside of the name. I would like to extract out the prod or nonprod from the group name for each row. Is there a regex that could do this?
Group name examples:
eap-edp-refined-nonprod-adp
eap-edp-reporting-prod-gcp
eap-edp-ingestion-nonprod-lunar
eap-edp-ingestion-prod-google
I would just want to extract prod/nonprod.

((?:non)?prod)
( = Start of capture group
(?:non) = None capturing group for the literal string "non"
? = Zero or one of "non"
prod = The literal string "prod"
) = End of capture group
Try it out at https://regex101.com/

Related

Regex to search two string inside a method

How to create a regex , so as to search two strings "greet" AND inside this method string "name" .
I tried
(^.*greet(\n|.|\t)*)(.*name*)
def greet(name):
print("Hello, " + name + ". Good morning!") <--- this name should be selected
def meet(name):
print("Lets meet, " + name )
I would use this regex:
greet([^\n]|\n+[^\S\n])*name
Here the strings greet and name are separated by characters that are not a linebreak ([^\n]) or, in the case, they must be eventually followed by a space that is not a linebreak ([^\S\n]). In this way you ensure that name is in the same method of greet.
See demo.
You can capture in a group what is between the parenthesis, and use a backreference \1 in the next line to match the same.
If you want to select it, you could also capture that in a group itself.
\bdef greet\(([^\s()]+)\):\r?\n.*(\1)
Regex demo
If it should be name only
\bdef greet\([^\s()]+\):\r?\n.*\b(name)\b
Regex demo

Regex to extract string between two symbols

I have a string like this
Affiliation / Facility Name = Provider 1069860 # Admissions = 1 #
Potentially Avoidable = 0
I want a Regex Expression to extract the value Provider 1069860 from it.
I tried "= [a-zA-Z]+ #" but it is giving a blank result
With this :
.*Facility Name = ([a-zA-Z 0-9]+) #.*
You match what you want in the match group one
https://regex101.com/r/EnYZ55/1

VBA regex and group

applying the below regex on below email body:
(pls[a-zA-Z0-9 .*-]*) \(([A-Z 0-9]*)\)
email body:
pls18244a.lam64*fra-pth (PI000581)
pls18856a.10ge*fra-pth (PI0005AW)
pls25040a.10ge*fra-pth (IIE0004WK)
pls27477a.10ge*fra-pth (WL050814)
pls22099a.stm4*par-pth (PI0005TE)
returns 5 match, with two groups. what is the VBA script to get groups in each match using using incremental variable to copy each match groups in excel row?
Not making any changes to your regular expression pattern. Using the following way, you can iterate through the groups of each match:
str="pls18244a.lam64*fra-pth (PI000581)pls18856a.10ge*fra-pth (PI0005AW)pls25040a.10ge*fra-pth (IIE0004WK)pls27477a.10ge*fra-pth (WL050814)pls22099a.stm4*par-pth (PI0005TE)"
Set objReg = New RegExp
objReg.IgnoreCase=False
objReg.Global=True
objReg.Pattern = "(pls[a-zA-Z0-9 .*-]*) \(([A-Z 0-9]*)\)"
Set objMatches = objReg.Execute(str)
For Each match In objMatches 'The variable match will contain the full match
a= match.Submatches.Count 'total number of groups in the full match
For i=0 To a-1
MsgBox match.Submatches.Item(i) 'display each group
Next
Next
Set objReg = Nothing

Regex on Splitting String

I am trying to split the following string into proper output using regex. Answers do not have to be in perl but in general regex is fine:
Username is required.
Multi-string name is optional
Followed by Uselessword is there but should be be parsed
Followed by an optional number
Following by an IP in brackets < > (Required)
String = username optional multistring name uselessword 45 <100.100.100.100>
Output should be:
Match 1 = username
Match 2 = optional multistring name
Match 3 = 45
Match 4 = 100.100.100.100
This sort of things are easier to handle using multiple regex. Here is an example:
my #arr = (
'username optional multistring name uselessword 45 <100.100.100.100>',
'username 45 <100.100.100.100>'
);
for(#arr){
## you can use anchor ^ $ here
if(/(\S+) (.+?) (\d+) <(.+?)>/){
print "$1\n$2\n$3\n$4\n";
}
## you can use anchor ^ $ here
elsif(/(\S+) (\d+) <(.+?)>/){
print "$1\n$2\n\n$3\n";
}
print "==========\n";
}
First if block is looking for four groups from the input. And the second block is looking for three groups.
If you need, you can use [ ]+ to handle multiple spaces between the groups.
Also, if you need, you can adjust the optional group (.+?) according to your preferred characters(usually through the character class [bla]).

Optional grouping in Scala Regular Expressions

Say I want to make a regex that splits a optional version number from a file name i.e
val regex(name, ver) = "file.jar" // name = file, ver = empty
val regex(name, ver) = "some-software.jar" // name = some-software, ver = empty
val regex(name, ver) = "software-1.0.jar" // name = software, ver = 1.0
val regex(name, ver) = "some-file-1.0.jar" // name = some-file, ver = 1.0
How is such a regular expression written in Scala/Java ?. In perl I would do something along the lines of:
(.*)(-(\d|.)*)?.jar
but Scala does not seam to support making optional groups in this syntax.
I am not sure what your question now is.
I assume it is not matching the second group, because the first one is greedy and since the second is optional, the first matches everything.
Try this:
(.*?)(?:-(?=\d)(.*))?.jar
See it here on Regexr
I made the first group a lazy match with the .*?
The second group is a non capturing group (the one starting with (?:. You will find the name now in group 1 and the version in group 2.
I put a lookahead after the dash, so that it searches for a dash followed by a digit.
How about:
/^(.+?)(?:-([\d.]*))?\.jar$/
Assuming version is always a mixed of digits and dots.