1

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
7
  • @Chris: FYI, regular expressions are not part of the C# language - they're part of the .NET Framework. Commented Apr 7, 2010 at 19:45
  • 2
    "meta regex" ! man, you're in for a headache... Commented Apr 7, 2010 at 19:50
  • Did it occur to you that regular expressions describe a regular language? Which means they can't be themselves a regular language. Parsing regular expressions with regular expressions is therefore not possible. Commented Apr 7, 2010 at 19:59
  • @Tomalak: Ok . . . then pretend that string is not a regular expression and voila, now we can run a regular expression on it, but I very much appreciate your pompous response. Commented Apr 7, 2010 at 20:20
  • 2
    @Tomalak: that reasoning is incorrect. “Real” regular expressions are in fact regular, they form a regular language. Just as contex-free grammars can be described in a context free language (look at the Wikipedia article for the “Backus–Naur Form” to see a formal definition in BNF). The reason why “colloquial” regular expressions are no longer regular is because of nested grouping parentheses (although even these could be expressed using modern regexps which include non-regular stack extensions). Commented Apr 7, 2010 at 20:39

2 Answers 2

2
((?<=[<])([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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.