Skip to content

Optimize Regex #64329

Jan 26, 2022 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

For example, if I had:
(aaaaaa1)|(aaaaaa2)|(aaaaaa3)
it would be nice to get something like:
(aaaaaa)(1|2|3)

That's not a valid transformation, as it changes the number of capture groups and what each capture group would capture.

Now, if your question was instead about aaaaaa1|aaaaaa2|aaaaaa3, then .NET 7 will optimize that to the equivalent of aaaaaa[123], which you can see by looking at the code generated by the source generator:

                // Description:
                // ○ Match the string "aaaaaa".
                // ○ Match a character in the set [1-3].
    
                protected override bool FindFirstChar()
                {
                    int pos = base.runtextpos,

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@TonyValenti
Comment options

Answer selected by stephentoub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants