开发者

.NET Regular Expression for Regular Expression Parsing

开发者 https://www.devze.com 2022-12-26 15:16 出处:网络
I want to return matches from a regular expression string. The regex string is: (?<TICKER>[A-Z]+)(?<SPACE>\\\\s)(?<MONTH_ALPHA_ABBREV>Jan|Feb|Mar|Apr|May|Jun|Jul|Sep|Oct|Nov|Dec)(?&

I want to return matches from a regular expression string. The regex string is:

(?<TICKER>[A-Z]+)(?<SPACE>\\s)(?<MONTH_ALPHA_ABBREV>Jan|Feb|Mar|Apr|May|Jun|Jul|Sep|Oct|Nov|Dec)(?<SPACE开发者_运维技巧>\\s)(?<DAY>\\d+)(?<SPACE>\\s)(?<YEAR_LONG>[2][0][0-9][0-9])(?<SPACE>\\s)(?<STRIKE_DOLLAR>\\d+(?=[.]))[.](?<STRIKE_DECIMAL>(?<=[.])\\d+)(?<SPACE>\\s)(?<PUTCALL_LONG>Call|Put)

And I want to get matches for all of the group names and all of the items within square brackets (including the square brackets) outside of open and closed parenthesis. I have this regex:

((?<=[<])([A-Z]|[_])+(?=[>]))|(\\[.\\])

But this returns square bracket items within the parenthesis. To be more specific these are the matches I want from the regex at the top (keep in mind this needs to be flexible for any regex):

TICKER
SPACE
MONTH_ALPHA_ABBREV
SPACE
DAY
SPACE
YEAR_LONG
SPACE
STRIKE_DOLLAR
[.]
STRIKE_DECIMAL
SPACE
PUTCALL_LONG


((?<=[<])([A-Z]|[_])+(?=[>]))|(?<!\([^\)]*)\[[^\]]+\]

Also, use the @"" notation so you don't have to escape the backslashes (as you did in your example code). This puppy's illegible enough.


When you match your regex you can set the options to include RegexOptions.ExplicitCapture which will only capture named groups normally everything inside parentheses is captured. Then you can name all your capture groups using this format (?<captureGroupName>[insertRegExHere]). This allows you to capture whatever you like and use sensible names.

0

精彩评论

暂无评论...
验证码 换一张
取 消