Tagged Questions
75
votes
10answers
96k 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?
101
votes
12answers
69k 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
57
votes
7answers
149k 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 ...
57
votes
8answers
45k 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:
...
46
votes
3answers
77k views
Regular Expression to find a string included between two characters, while EXCLUDING the delimiters
I looked around for a while, but probably I can't "Google" with the proper keywords, so I'm asking here. I need to extract from a string a set of characters which are included between two delimiters, ...
67
votes
3answers
55k 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 ...
41
votes
11answers
18k 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 ...
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 ...
32
votes
12answers
72k views
How do I extract a string of text that lies between two (parenthesis) using .NET?
I have a string "User name (sales)" and I want to extract the text between the parenthesis, how would I do this? I suspect substring but I can't work out how to read until the closing bracket, the ...
29
votes
8answers
67k 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
43k 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 ...
8
votes
7answers
40k views
Regular expression for decimal number
I need to validate a textbox input and can only allow decimal inputs like:
X,XXX (only one digit before decimal sign and a precision of 3)
I'm using C#
Should it be something like ...
571
votes
3answers
41k views
\d is less efficient than [0-9]
I made a comment yesterday on an answer where someone had used [0123456789] in a regular expression rather than [0-9] or \d. I said it was probably more efficient to use a range or digit specifier ...
64
votes
17answers
19k 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 ...