简体   繁体   中英

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

Use:

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

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

Working on RegExr

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*).*$

Consider the following Regex...

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

Good Luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM