Replies: 104 comments 1 reply
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
@alrz i guess for me its a clarity issue because its not a "display" string a string that goes in logs, on the UI etc.. its code that executes. |
Beta Was this translation helpful? Give feedback.
-
I like this just for the precompilation into the assembly. |
Beta Was this translation helpful? Give feedback.
-
I like this alot. I would suggest using an 'R' prefix on the string to denote regexs though.
Again, I'm a huge fan of making regexs be compile-time-compiled. |
Beta Was this translation helpful? Give feedback.
-
Also, related to this, it would be awesome if named captures became properties or something like that so that I wouldn't have to use magic strings to find their values. |
Beta Was this translation helpful? Give feedback.
-
Is there a reason an analyzer would not be sufficient here? |
Beta Was this translation helpful? Give feedback.
-
For strings used directly in regex contexts, an analyzer could easily check it. For strings intended to be used for regexs later you could just have: public static class RegexAnalyzer
{
public static string R(string v) => v;
}
...
import static RegexAnalyzer;
...
R("myregexp"); And then have your analyzer check R("...") calls. That's effectively only two chars more than the R"..." approach. |
Beta Was this translation helpful? Give feedback.
-
The main advantage is the compile-time compilation, because even if you cache a |
Beta Was this translation helpful? Give feedback.
-
How would you propose that work? |
Beta Was this translation helpful? Give feedback.
-
AFAIK |
Beta Was this translation helpful? Give feedback.
-
Wouldn't that require that the compiler and the runtime both have the exact same implementation of a compiled regex? |
Beta Was this translation helpful? Give feedback.
-
@yaakov-h If we take |
Beta Was this translation helpful? Give feedback.
-
It would mean that the C# spec would have to consume the entirety of the .NET-specific dialect of the regex spec. This is even assuming that the BCL code to dynamically generate assemblies from regex is something that could be reasonably consumed by Roslyn without some serious rewriting. Having better analyzers to verify syntax would be a good thing. As would having an ahead-of-time compilation of regex, similar to |
Beta Was this translation helpful? Give feedback.
-
Pardon my ignorance on the details on how the IL/CLR would work with something like this but I do see a benefit: Would it be possible to make the references a compile-time constant like how
|
Beta Was this translation helpful? Give feedback.
-
That would mean that the compiler/spec would have to track all of the variants of every possible paired piece of punctuation allowed? How far would that go?
Would seem a bit overkill and tricky. Some cultures even invert their marks, e.g. French This is probably why C++ includes the terminator within the string. It eliminates the need for the compiler to be concerned about any specific parsing of that character sequence. Could be an identifier, punctuation, whatever: string raw_str=R"ABC«.*(This is the raw string content)ABC«.*"; No cute matching of brackets, though. |
Beta Was this translation helpful? Give feedback.
-
any punctuation? |
Beta Was this translation helpful? Give feedback.
-
My favorite would be Khmer: |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Pfshaw! We can do better than those guys!
Pfshaw! We can do better than those guys! Think big! |
Beta Was this translation helpful? Give feedback.
-
FYI: I may still be carrying my new years 'exuberance' over to today... :D |
Beta Was this translation helpful? Give feedback.
-
Apparently... |
Beta Was this translation helpful? Give feedback.
-
Can we close this thread and move the discussion on the new string literal syntax somewhere else? I’m only here for the regex support. |
Beta Was this translation helpful? Give feedback.
-
@daniel-white Regex support is over at: dotnet/roslyn#23984 |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Please add some artistic value into the proposals. string veryRaw = <*/((((><| Content could go before the tail. |
Beta Was this translation helpful? Give feedback.
-
A while back I wrote a Fody build plugin that essentially implements this behavior: https://github.com/madelson/PrecompiledRegex.Fody. May be of interest to those who would find this language feature useful. My worry with Regex literals is that there are just too many options I often want to specify (e. g. ExplicitCapture, IgnoreCase, Timeout, Multi/Single line modes) and it would be hard to encode those into the literal in a way that retained the readability of what we have today. Since having this library available to me, I've also noticed that it has been less helpful in my own projects that I would have thought: too often I find I'm building my patterns dynamically rather that statically. While in theory a regex literal syntax could allow for dynamic insertions, unless those are always escaped this would turn off compile-time validation, strong-typing of capture groups, etc. Another problem I've encountered is that compiled regex IL can get very bloated for complex patterns, in some cases causing noticeable startup stalls due to what I can only assume to be JIT compilation of the massive methods. Presumably this could be addressed with changes to the regex compilation logic. |
Beta Was this translation helpful? Give feedback.
-
Regex shouldn't be builtins. There are plenty of examples when regex evolved and with time one libraries were replaced with others. For example in C++ it was runtime regexes but then they came to compile-time regexes. Rust doesn't even have them at std, they are shipped as 3rd party library. There is literally no reason to add this into the language. It's somewhat trashed already. |
Beta Was this translation helpful? Give feedback.
-
What fraction of all C# lines of code deal with instantiating a regex? It is a very small fraction. Is it really worth adding this to the language? I cannot imagine it is. In my opinion, managing regular expressions is not a significant pain point at the moment. Could be better but it's fine. |
Beta Was this translation helpful? Give feedback.
-
There is some advantage to this proposal – being able to cache the regex object constructed from the pattern. However, this is also possible with my static expressions proposal, see #3929: if((static new Regex("...", RegexOptions.Compiled)).IsMatch( ... )) The instance should be cached as a static field and so will not have to be created each time. This works well with other regex engines and similar singleton objects. Most code analyzers will highlight issues in the pattern anyway, and if that slips by, the type initializer should catch the rest. |
Beta Was this translation helpful? Give feedback.
-
Moved from dotnet/roslyn#5806
can be compiled into the resultant assembly, so an invalid pattern will be a compile-time error.
It might be useful to make it implicitly verbatim to avoid double-escape in regular usage.
Also, we should be able to use it as a pattern against strings,
If language understand regex, we might be able to use string interpolations to produce parameterized regex patterns, e.g.
~$"a{{{count}}}"
orif (str is ~$"{int number:\d+}")
.Beta Was this translation helpful? Give feedback.
All reactions