Mike Flynn
Mike Flynn

Reputation: 24325

Regex Exclude Part Of String On Capture C#

I am trying to do a url rewrite but can't capture the correct action for two scenarios. The first is if the action is "scheduletable", only capture "schedule" in "action", otherwise capture the whole thing "pools". My regex keeps including the "table" in the "action"

^/events/(?<action>.*)(?table)?/(?<eventid>\d*).*$

The original URL

/events/scheduletable/39?layout=time&type=all
/events/pools/39

Upvotes: 1

Views: 71

Answers (3)

gpmurthy
gpmurthy

Reputation: 2427

Consider the following Regex...

(?<=events/).*?(?=(table|/))

Good Luck!

Upvotes: 0

Tim S.
Tim S.

Reputation: 56536

You had a ? in the wrong place. (?table) doesn't make sense, and your * on action needs to be non-greedy (.*? instead of .*). Try this;

^/events/(?<action>.*?)(table)?/(?<eventid>\d*).*$

Upvotes: 0

OGHaza
OGHaza

Reputation: 4795

Use:

^/events/(?<action>.*?)(?:table)?/(?<eventid>\d*).*$

And use $1 and $2 in the replacement - or your group names even.

Working on RegExr

Upvotes: 2

Related Questions