Your regex runs on every request. It also recompiles on every request - unless you do something about it. `Regex.IsMatch` handles the quick yes/no check. Named groups let you pull out values by name instead of counting parentheses like it's 1999. And `[GeneratedRegex]`, shipped in .NET 7, compiles your pattern straight to IL at build time - zero runtime overhead, and your profiler finally stops yelling at you. --- var match = Regex.Match(log, @"user=(?<email>[\w.]+@[\w.]+)\s+action=(?<action>\S+)"); Console.WriteLine(match.Groups["email"].Value); // no index guessing --- Microsoft made regex fast by default. Only took until 2022. Try it yourself (no setup required): https://lnkd.in/eg68aQiZ #dotnet #csharp #programming #regex
Regex in .NET 7: Faster and More Efficient
More Relevant Posts
-
📅 Day 12: 𝒑𝒓𝒊𝒏𝒕𝒇 is 53 years old. cout chaining is an eyesore. C++20 finally fixed string formatting. We've all written it. The cout chain that wraps across three lines just to print two values. Or the 𝒑𝒓𝒊𝒏𝒕𝒇 format string, where a single wrong specifier %𝒅 instead of %𝒍𝒅 silently corrupts your output or crashes at runtime. No compiler warning. No type checking. Just undefined behavior waiting to happen. 𝒔𝒕𝒅::𝒇𝒐𝒓𝒎𝒂𝒕 lands in C++20 and changes everything. Python-style {} placeholders, fully type-safe, no format specifiers to memorize. The compiler knows what type you're passing. It formats it correctly. End of story. And it's not just cleaner, it's faster. iostream carries significant overhead from locale handling and the synchronized stream machinery. 𝒔𝒕𝒅::𝒇𝒐𝒓𝒎𝒂𝒕 skips all of that. Benchmarks consistently put it ahead of cout for string construction, often significantly. 🧠 Key insight: printf is unsafe because the format string and the arguments are disconnected, and the compiler can't match them. 𝒄𝒐𝒖𝒕 is safe, but composing output is syntactic noise. 𝒔𝒕𝒅::𝒇𝒐𝒓𝒎𝒂𝒕 gives you the readability of one and the safety of the other, with better performance than both. Worth knowing: 𝒔𝒕𝒅::𝒇𝒐𝒓𝒎𝒂𝒕 returns a 𝒔𝒕𝒅::𝒔𝒕𝒓𝒊𝒏𝒈. Use 𝒔𝒕𝒅::𝒑𝒓𝒊𝒏𝒕 (C++23) to write directly to stdout without constructing one. Format specifiers still exist for precision and alignment {:.2𝒇}, {:>10}, but you only reach for them when you need them. · Custom types can opt in via a 𝒔𝒕𝒅::𝒇𝒐𝒓𝒎𝒂𝒕𝒕𝒆𝒓<𝑻> specialization of your types, your formatting rules. · Not on C++20 yet? The {𝒇𝒎𝒕} library is the identical open-source predecessor. Same API, drop-in ready. Write format strings that read like sentences. Not code that reads like noise. Day 12 of my C++ deep-dive series. Missed Day 11? Go check out the composition over inheritance breakdown. Still on printf or cout in your codebase? What's blocking the move to 𝒔𝒕𝒅::𝒇𝒐𝒓𝒎𝒂𝒕? 👇 #cpp #cplusplus #cpp20 #programming #softwareengineering
To view or add a comment, sign in
-
-
Source Generators let the compiler write your boilerplate at build time - real C# files, zero reflection, zero runtime cost. They inspect your code during compilation and emit new source files that get compiled right alongside yours. It's like having an intern who only writes the tedious parts and never asks for a code review. // You write the model. The generator writes the rest: public partial class PizzaOrder { public string Topping { get; set; } } // This partial was never typed by a human: public override string ToString() => $"PizzaOrder {{ Topping = {Topping}, Qty = {Quantity}, Price = {Price:C} }}"; Microsoft introduced them in .NET 5 / C# 9 (2020) to kill reflection-based slowness and make things like `System.Text.Json` actually fast. Writing code that writes code - still feels like cheating, honestly. See it in action: https://lnkd.in/eb8S_rXV #dotnet #csharp #programming #sourcegenerators
To view or add a comment, sign in
-
-
You ever stare at a regex match and try to figure out what 'group[3]' actually means? Yeah, same. Named capture groups let you label parts of your pattern so 'match.Groups["date"]' actually tells you something. Pair that with 'Regex.IsMatch' for quick yes/no checks, and you suddenly feel like you know what you are doing. Introduced back in .NET 1.1, regex named groups have been quietly saving developers from their own cryptic patterns for over two decades. .NET 7 went further with source-generated regex - compile-time validation so you break at build, not at 2am. It is like spell-check, but for your paranoia. --- var pattern = @"(?<date>\d{4}-\d{2}-\d{2}) (?<level>\w+)"; var match = Regex.Match(logLine, pattern); Console.WriteLine(match.Groups["date"].Value); --- Run it, break it, learn it: https://lnkd.in/e-_w4N-f #dotnet #csharp #programming #regex
To view or add a comment, sign in
-
-
Your compiler has been secretly capable of writing code for you this whole time. Source Generators are a Roslyn feature that run during compilation and inject code directly into your build - no reflection, no runtime overhead, no excuses. You write a partial class, the generator writes the other half. By the time your app runs, it's all just plain compiled C#. Spooky fast. Microsoft shipped them in .NET 5 (2020), mostly to fix the "why is JSON serialization slow" complaints. Spoiler: it was reflection. It's always reflection. --- // You write this: public partial class PizzaOrder { ... } // The generator writes this at compile time: public string Describe() => $"{Quantity}x {Topping} - your arteries called"; --- Try it yourself (no setup required): https://lnkd.in/emee3pA8 #dotnet #csharp #programming #roslyn
To view or add a comment, sign in
-
-
Part 3 of Routing Back to C is up. A hash table in plain C with chaining, buckets .... and a lot more https://lnkd.in/dG-nvQFJ [Part 3] #C #Programming #GameDev #LLM #LearnInPublic
To view or add a comment, sign in
-
Post No: 048 A small but interesting thing I recently got to know in C++ is how static_cast behaves with string literals. When I write “hello”, I thought it is a std::string, but it is not. A string literal in C++ is actually of type const char[]. In most expressions, this array decays into a pointer, which is why: auto text = “hello”; makes text a const char*, not a std::string. This is also why static_cast may seem to “cast it to a pointer”. What is really happening is array-to-pointer conversion. The important thing to understand is that std::string and std::string_view are class types. To create them, we need object construction. For std::string: std::string str = static_cast<std::string>(“hello”); For std::string_view: std::string_view sv = static_cast<std::string_view>(“hello”); We can also directly construct them in a cleaner way: std::string str(“hello”); std::string_view sv(“hello”); I got to learn this in a hard way, hope this makes things more easy for someone else. #cpp #cplusplus #programming #softwaredevelopment #coding #learning
To view or add a comment, sign in
-
Most developers think: “If the code works… then it’s fine.” But in reality: Working code ≠ Good code. In this video, I walk through a real .NET scenario: - Fetching users by department - Building a report using LINQ - Returning the result as a file Then I show: ❌ A version that works but has hidden problems ✔️ A corrected version using best practices We cover: - IEnumerable vs IQueryable - Why ToList() placement matters - String vs StringBuilder - Filtering in memory vs database - Clean and scalable code structure 💡 Key takeaway: Your code might work today… but bad practices will hurt you when your data grows. Watch the video In comments and see the difference yourself 👇 #dotnet #csharp #webapi #backend #softwareengineering #cleanCode #programming
To view or add a comment, sign in
-
-
You are still writing five-level if/else blocks to handle types and conditions. C# 8 brought switch expressions with property patterns, and they have been sitting there ever since, quietly judging you. Instead of checking properties one by one, you match against the whole shape of an object in one clean expression. --- var price = order switch { { Drink: "Espresso", ExtraShot: true } => 4.50, { Drink: "Latte", Size: <= 12 } => 3.75, { Size: >= 20 } => 6.00, _ => 2.50 }; --- Shipped in C# 8 (2019) and quietly upgraded every version since - relational patterns, nested patterns, you name it. Microsoft kept cooking on this one. Run it, break it, learn it: https://lnkd.in/eqSfeq5T #dotnet #csharp #programming #cleancode
To view or add a comment, sign in
-
-
💡𝐂#/.𝐍𝐄𝐓 𝐀𝐬𝐲𝐧𝐜 𝐎𝐩𝐞𝐫𝐚𝐭𝐢𝐨𝐧 𝐓𝐢𝐩 🚀 💎𝐇𝐨𝐰 𝐚𝐧𝐝 𝐰𝐡𝐞𝐧 𝐭𝐨 𝐮𝐬𝐞 ‘𝐚𝐬𝐲𝐧𝐜’ 𝐚𝐧𝐝 ‘𝐚𝐰𝐚𝐢𝐭’ 💡 '𝐚𝐬𝐲𝐧𝐜' and '𝐚𝐰𝐚𝐢𝐭' keywords introduced in C# 5.0 were designed to make it easier to write asynchronous code, which can run in the background while other code is executing. The "async" keyword marks a method asynchronous, meaning it can be run in the background while another code executes. ⚡ When using 𝐚𝐬𝐲𝐧𝐜 and 𝐚𝐰𝐚𝐢𝐭 the compiler generates a state machine in the background. 🔥 Let's look at the other high-level details in the example; 🔸 𝐓𝐚𝐬𝐤<𝐢𝐧𝐭> 𝐥𝐨𝐧𝐠𝐑𝐮𝐧𝐧𝐢𝐧𝐠𝐓𝐚𝐬𝐤 = 𝐋𝐨𝐧𝐠𝐑𝐮𝐧𝐧𝐢𝐧𝐠𝐎𝐩𝐞𝐫𝐚𝐭𝐢𝐨𝐧𝐀𝐬𝐲𝐧𝐜(); starts executing 𝐋𝐨𝐧𝐠𝐑𝐮𝐧𝐧𝐢𝐧𝐠𝐎𝐩𝐞𝐫𝐚𝐭𝐢𝐨𝐧. 🔸 Independent work is done on let's assume the Main Thread (Thread ID = 1) then 𝐚𝐰𝐚𝐢𝐭 𝐥𝐨𝐧𝐠𝐑𝐮𝐧𝐧𝐢𝐧𝐠𝐓𝐚𝐬𝐤 is reached. 🔸 Now, if the 𝐥𝐨𝐧𝐠𝐑𝐮𝐧𝐧𝐢𝐧𝐠𝐓𝐚𝐬𝐤 hasn't finished and it is still running, 𝐃𝐨𝐒𝐨𝐦𝐞𝐭𝐡𝐢𝐧𝐠𝐀𝐬𝐲𝐧𝐜() will return to its calling method, this the main thread doesn't get blocked. When the 𝐥𝐨𝐧𝐠𝐑𝐮𝐧𝐧𝐢𝐧𝐠𝐓𝐚𝐬𝐤 is done then a thread from the ThreadPool (can be any thread) will return to 𝐃𝐨𝐒𝐨𝐦𝐞𝐭𝐡𝐢𝐧𝐠𝐀𝐬𝐲𝐧𝐜() in its previous context and continue execution (in this case printing the result to the console). ✅ A second case would be that the 𝐥𝐨𝐧𝐠𝐑𝐮𝐧𝐧𝐢𝐧𝐠𝐓𝐚𝐬𝐤 has already finished its execution and the result is available. When reaching the 𝐚𝐰𝐚𝐢𝐭 𝐥𝐨𝐧𝐠𝐑𝐮𝐧𝐧𝐢𝐧𝐠𝐓𝐚𝐬𝐤 we already have the result so the code will continue executing on the very same thread. (in this case printing result to console). Of course this is not the case for in the example, where there's a 𝐓𝐚𝐬𝐤.𝐃𝐞𝐥𝐚𝐲(1000) involved. 🎯 𝐖𝐡𝐚𝐭 𝐝𝐨 𝐲𝐨𝐮 𝐭𝐡𝐢𝐧𝐤 𝐚𝐛𝐨𝐮𝐭 𝐚𝐬𝐲𝐧𝐜 𝐨𝐩𝐞𝐫𝐚𝐭𝐢𝐨𝐧𝐬? #csharp #dotnet #programming #softwareengineering #softwaredevelopment
To view or add a comment, sign in
-
-
💡𝐂#/.𝐍𝐄𝐓 𝐀𝐬𝐲𝐧𝐜 𝐎𝐩𝐞𝐫𝐚𝐭𝐢𝐨𝐧 𝐓𝐢𝐩 🚀 💎𝐇𝐨𝐰 𝐚𝐧𝐝 𝐰𝐡𝐞𝐧 𝐭𝐨 𝐮𝐬𝐞 ‘𝐚𝐬𝐲𝐧𝐜’ 𝐚𝐧𝐝 ‘𝐚𝐰𝐚𝐢𝐭’ 💡 '𝐚𝐬𝐲𝐧𝐜' and '𝐚𝐰𝐚𝐢𝐭' keywords introduced in C# 5.0 were designed to make it easier to write asynchronous code, which can run in the background while other code is executing. The "async" keyword marks a method asynchronous, meaning it can be run in the background while another code executes. ⚡ When using 𝐚𝐬𝐲𝐧𝐜 and 𝐚𝐰𝐚𝐢𝐭 the compiler generates a state machine in the background. 🔥 Let's look at the other high-level details in the example; 🔸 𝐓𝐚𝐬𝐤<𝐢𝐧𝐭> 𝐥𝐨𝐧𝐠𝐑𝐮𝐧𝐧𝐢𝐧𝐠𝐓𝐚𝐬𝐤 = 𝐋𝐨𝐧𝐠𝐑𝐮𝐧𝐧𝐢𝐧𝐠𝐎𝐩𝐞𝐫𝐚𝐭𝐢𝐨𝐧𝐀𝐬𝐲𝐧𝐜(); starts executing 𝐋𝐨𝐧𝐠𝐑𝐮𝐧𝐧𝐢𝐧𝐠𝐎𝐩𝐞𝐫𝐚𝐭𝐢𝐨𝐧. 🔸 Independent work is done on let's assume the Main Thread (Thread ID = 1) then 𝐚𝐰𝐚𝐢𝐭 𝐥𝐨𝐧𝐠𝐑𝐮𝐧𝐧𝐢𝐧𝐠𝐓𝐚𝐬𝐤 is reached. 🔸 Now, if the 𝐥𝐨𝐧𝐠𝐑𝐮𝐧𝐧𝐢𝐧𝐠𝐓𝐚𝐬𝐤 hasn't finished and it is still running, 𝐃𝐨𝐒𝐨𝐦𝐞𝐭𝐡𝐢𝐧𝐠𝐀𝐬𝐲𝐧𝐜() will return to its calling method, this the main thread doesn't get blocked. When the 𝐥𝐨𝐧𝐠𝐑𝐮𝐧𝐧𝐢𝐧𝐠𝐓𝐚𝐬𝐤 is done then a thread from the ThreadPool (can be any thread) will return to 𝐃𝐨𝐒𝐨𝐦𝐞𝐭𝐡𝐢𝐧𝐠𝐀𝐬𝐲𝐧𝐜() in its previous context and continue execution (in this case printing the result to the console). ✅ A second case would be that the 𝐥𝐨𝐧𝐠𝐑𝐮𝐧𝐧𝐢𝐧𝐠𝐓𝐚𝐬𝐤 has already finished its execution and the result is available. When reaching the 𝐚𝐰𝐚𝐢𝐭 𝐥𝐨𝐧𝐠𝐑𝐮𝐧𝐧𝐢𝐧𝐠𝐓𝐚𝐬𝐤 we already have the result so the code will continue executing on the very same thread. (in this case printing result to console). Of course this is not the case for in the example, where there's a 𝐓𝐚𝐬𝐤.𝐃𝐞𝐥𝐚𝐲(1000) involved. 🎯 𝐖𝐡𝐚𝐭 𝐝𝐨 𝐲𝐨𝐮 𝐭𝐡𝐢𝐧𝐤 𝐚𝐛𝐨𝐮𝐭 𝐚𝐬𝐲𝐧𝐜 𝐨𝐩𝐞𝐫𝐚𝐭𝐢𝐨𝐧𝐬? #csharp #dotnet #programming #softwareengineering #softwaredevelopment
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development