Tagged Questions
70
votes
10answers
89k views
Using C# regular expressions to remove HTML tags
How do I use C# regular expression to replace/remove all HTML tags, including the angle brackets?
Can someone please help me with the code?
96
votes
11answers
64k views
How do I replace multiple spaces with a single space in C#?
How can I replace multiple spaces in a string with only one space in C#?
Example:
1 2 3 4 5
would be:
1 2 3 4 5
53
votes
7answers
132k views
Regex for numbers only
I haven't used regular expressions at all, so I'm having difficulty troubleshooting. I want the regex to match only when the contained string is all numbers; but with the two examples below it is ...
53
votes
8answers
40k views
How can I strip HTML tags from a string in ASP.NET?
Using ASP.NET, how can I strip the HTML tags from a given string reliably (i.e. not using regex)? I am looking for something like PHP's strip_tags.
Example:
...
40
votes
11answers
17k views
Regular Expression to split on spaces unless in quotes
I would like to use the .Net Regex.Split method to split this input string into an array. It must split on whitespace unless it is enclosed in a quote.
Input:
Here is "my ...
57
votes
3answers
48k views
Regex: Named Capturing Groups in .NET
I'm having a hard time finding a good resource that explains how to use Named Capturing Groups in C#. This is the code that I have so far:
string page = Encoding.ASCII.GetString(bytePage);
Regex ...
16
votes
7answers
2k views
When not to use Regex in C# (or Java, C++, etc.)
It is clear that there are lots of problems that look like a simple regex expression will solve, but which prove to be very hard to solve with regex.
So how does someone that is not an expert in ...
15
votes
3answers
3k views
Overlapping matches in Regex
I can't seem to find an answer to this problem, and I'm wondering if one exists. Simplified example:
Consider a string "nnnn", where I want to find all matches of "nn" - but also those that overlap ...
27
votes
12answers
65k views
How do I extract a string of text that lies between two (brackets) using .NET?
I have a string "User name (sales)" and I want to extract the text between the brackets, how would I do this? I suspect substring but I can't work out how to read until the closing bracket, the length ...
57
votes
15answers
18k views
Add spaces before Capital Letters
Given the string "ThisStringHasNoSpacesButItDoesHaveCapitals" what is the best way to add spaces before the capital letters. So the end string would be "This String Has No Spaces But It Does Have ...
26
votes
8answers
10k views
How do I filter all HTML tags except a certain whitelist?
This is for .NET. IgnoreCase is set and MultiLine is NOT set.
Usually I'm decent at regex, maybe I'm running low on caffeine...
Users are allowed to enter HTML-encoded entities (<lt;, <amp;, ...
25
votes
7answers
55k views
c# regex email validation
I use this
@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"
regexp to validate the email
([\w\.\-]+) - this is for the first-level domain (many letters and numbers, also point and hyphen)
([\w\-]+) - ...
22
votes
10answers
40k views
Regular expression for validating names and surnames?
Although this seems like a trivial question, I am quite sure it is not :)
I need to validate names and surnames of people from all over the world. How can I do that with a regular expression? If it ...
13
votes
5answers
5k views
C# code to linkify urls in a string
Does anyone have any good c# code (and regular expressions) that will parse a string and "linkify" any urls that may be in the string?
6
votes
7answers
24k views
C#, regular expressions : how to parse comma-separated values, where some values might be quoted strings themselves containing commas
In C#, using the Regex class, how does one parse comma-separated values, where some values might be quoted strings themselves containing commas?
using System ;
using System.Text.RegularExpressions ;
...