C
C#12mo ago
mrdudebro1

Regex Group Contains

hey simple easy question, but are there ways to access individual capture groups in a regex match? I want to say
if(regex.Group(2).contains("401") {
//do some stuff with
}
if(regex.Group(2).contains("401") {
//do some stuff with
}
13 Replies
TheRanger
TheRanger12mo ago
regexMatch.Groups[2].Value.Contains("401")
mrdudebro1
mrdudebro1OP12mo ago
thanks brotha looks like a match class word
Omnissiah
Omnissiah12mo ago
you could consider using named groups (?<name>pattern)
TheRanger
TheRanger12mo ago
if you did that you can access the group like this
regexMatch.Groups["name"].Value.Contains("401")
regexMatch.Groups["name"].Value.Contains("401")
mrdudebro1
mrdudebro1OP12mo ago
perf i appreciate it guys that looks like its in the actual regex declaration? hang on
^(<name>(ADIP)(\\d*)?([/])?([A-Za-z])*(\\d)*(\\.)?(\\d{1,2}))$
^(<name>(ADIP)(\\d*)?([/])?([A-Za-z])*(\\d)*(\\.)?(\\d{1,2}))$
is that right?
TheRanger
TheRanger12mo ago
u forgot a ? before <name>
mrdudebro1
mrdudebro1OP12mo ago
oops
^(?<name>(ADIP)?<name2>(\\d*)??<name3>([/])?([A-Za-z])*(\\d)*(\\.)?(\\d{1,2}))$
^(?<name>(ADIP)?<name2>(\\d*)??<name3>([/])?([A-Za-z])*(\\d)*(\\.)?(\\d{1,2}))$
huh there's 2 questions marks in there now. i can do some debugging no worries
Omnissiah
Omnissiah12mo ago
something doesn't look right
mrdudebro1
mrdudebro1OP12mo ago
yeah i already have a star in there i should probably remove the first question mark lol idk tbh i can't debug atm i will later
^(?<name>(ADIP)(?<name2>(\d*))(?<name3>([/]))?([A-Za-z])*(\d)*(\.)?(\d{1,2}))$
^(?<name>(ADIP)(?<name2>(\d*))(?<name3>([/]))?([A-Za-z])*(\d)*(\.)?(\d{1,2}))$
Omnissiah
Omnissiah12mo ago
i mean there are no ( before name2 and name3 ah ok
mrdudebro1
mrdudebro1OP12mo ago
just had to put parantehses around it lmao
Omnissiah
Omnissiah12mo ago
also why capture the whole name group, it's just constant+name2
mrdudebro1
mrdudebro1OP12mo ago
ope didn't mean to do that tbh
Want results from more Discord servers?
Add your server