tags:

views:

117

answers:

4

Hi,

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]

+2  A: 
// 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), "");
Timwi
+2  A: 

Use the regexp divider |, e.g.:

</?html>|</?head>|</?div>
jerone
+1  A: 

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;
        }
snurre
+1  A: 

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);
Alan Moore
Hi guys, thank you all for your response! I've got it fixed thanks to you guys! Cheers.
Yustme