C
C#2y 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
TheRanger2y ago
regexMatch.Groups[2].Value.Contains("401")
mrdudebro1
mrdudebro1OP2y ago
thanks brotha looks like a match class word
many things
many things2y ago
you could consider using named groups (?<name>pattern)
TheRanger
TheRanger2y 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
mrdudebro1OP2y 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
TheRanger2y ago
u forgot a ? before <name>
mrdudebro1
mrdudebro1OP2y 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
many things
many things2y ago
something doesn't look right
mrdudebro1
mrdudebro1OP2y 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}))$
many things
many things2y ago
i mean there are no ( before name2 and name3 ah ok
mrdudebro1
mrdudebro1OP2y ago
just had to put parantehses around it lmao
many things
many things2y ago
also why capture the whole name group, it's just constant+name2
mrdudebro1
mrdudebro1OP2y ago
ope didn't mean to do that tbh

Did you find this page helpful?