How can i pass multiple patterns to the regex.replace() pattern parameter?
In PHP you just give up the array containing them. Is there some kind of same option in C#?
I'm looping through some usernames and some of those usernames are surrounded by html tags. The html tags are not all the same. But i do know which ones they are.
So if i can pass multiple patterns to look for in the regex.replace() pattern parameter, would be nice. Or i'll have to make for each html tag a separate pattern and run the regex.replace() function.
Hope i'm clear about what i'm trying to accomplish!
Thanks in advance!开发者_如何学编程
[EDIT] @Alan Moore,
Bottom line, removing all html tags out of a string, is what i'm trying to do.
[/EDIT]
// I’m assuming you have a regex for each thing you want to replace
List<string> multiplePatterns = [...];
string result = Regex.Replace(input, string.Join("|", multiplePatterns), "");
Use the regexp divider |
, e.g.:
</?html>|</?head>|</?div>
So you have a list of strings, each consisting entirely of a user name optionally enclosed in HTML tags? It shouldn't matter which tags they are; just remove anything that looks like a tag:
name = Regex.Replace(name, @"</?\w+[^<>]*>", String.Empty);
If performance is important and there's no risk of '<' or '>' in user names, this could work:
string RemoveTags(string s)
{
int startIndex = s.IndexOf('<');
int endIndex = s.IndexOf('>');
return startIndex >= 0 && endIndex >= 0 ? RemoveTags(s.Remove(startIndex, endIndex - startIndex + 1)) : s;
}
精彩评论