Regex to extract string between two symbols - regex

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

Related

Regex to parse out substring, two options

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/

Powershell Regex expression to get part of a string

I would like to take part of a string to use it elsewhere. For example, I have the following strings:
Project XYZ is the project name - 20-12-11
I would like to get the value "XYZ is the project name" from the string. The word "Project" and character "-" before the number will always be there.
I think a lookaround regular expression would work here since "Project" and "-" are always there:
(?<=Project ).+?(?= -)
A lookaround can be useful for cases that deal with getting a sub string.
Explanation:
(?<= = negative lookbehind
Project = starting string (including space)
) = closing negative lookbehind
.+? = matches anything in between
(?= = positive lookahead
- = ending string
) = closing positive lookahead
Example in PowerShell:
Function GetProjectName($InputString) {
$regExResult = $InputString | Select-String -Pattern '(?<=Project ).+?(?= -)'
$regExResult.Matches[0].Value
}
$projectName = GetProjectName -InputString "Project XYZ is the project name - 20-12-11"
Write-Host "Result = '$($projectName)'"
here is yet another regex version. [grin] it may be easier to understand since it uses somewhat basic regex patterns.
what it does ...
defines the input string
defines the prefix to match on
this will keep only what comes after it.
defines the suffix to match on
this part will keep only what is before it.
trigger the replace
the part in the () is what will be placed into the 1st capture group.
show what was kept
the code ...
$InString = 'Project XYZ is the project name - 20-12-11'
# "^" = start of string
$Prefix = '^project '
# ".+' = one or more of any character
# "$" = end of string
$Suffix = ' - .+$'
# "$1" holds the content of the 1st [and only] capture group
$OutString = $InString -replace "$Prefix(.+)$Suffix", '$1'
$OutString
# define the input string
$str = 'Project XYZ is the project name - 20-12-11'
# use regex (-match) including the .*? regex pattern
# this patterns means (.)any char, (*) any times, (?) maximum greed
# to capture (into brackets) the desired pattern substring
$str -match "(Project.*?is the project name)"
# show result (the first capturing group)
$matches[1]

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.

RegEx to parse ID3 tags

I am making a mp3 id3tag editor, and a regex is not matching.
Could anyone help me please?
my code:
arquivo = "[coletanea] album [CD #] [faixa] [artista] musica.mp3"
r = New Regex("^\[(?<1>[^\]]+?)\]\s*(?<2>[\w\s]+)\s*\[CD\s*(?<3>\d+)\]\s*\[(?<4>\d+)\]\s*\[(?<5>[^\]]+)\]\s*(?<6>.+)", RegexOptions.Compiled)
m = r.Match(Mid(arquivo, 1, Len(arquivo) - 4))
If m.Success Then
mAuthor = Trim(m.Groups(5).ToString)
mWM_AlbumTitle = Trim(m.Groups(2).ToString)
mWM_TrackNumber = Trim(m.Groups(4).ToString)
mTitle = Trim(m.Groups(6).ToString)
mWM_PartOfSet = Trim(m.Groups(3).ToString)
mMW_AlbumArtist = Trim(m.Groups(1).ToString)
End If
That's because your string will never match the regexp you are using.
The regexp expects a number instead of # and a number instead of 'faixa'
Try this, for example:
"[coletanea] album [CD 20] [89] [artista] musica.mp3"
If you wish to allow for any character instead of just numbers, replace \d for . in the groups 3 and 4
Well, your string dosen't match the regexp (since '#' and 'faixa' is not digits) try with this string witch I'm guessing is the string you will get from the system:
"[coletanea] album [CD 1] [1] [artista] musica.mp3"
Its also a little weird to name the capture groups to numbers like this, because the groups will be named starting at 0 by default.