views:

91

answers:

2

I've designed a multilingual web site and some values in database have a tag which will be replaced with it's language value, remove tags brackets ( in case {} ) or removed completely.

There are two cases:

Remove brackets:

value {mm} >> value mm

Remove completely:

value {mm} >> value

Also {tag} could be any length and can contain -

Can anybody help me with regex?

+2  A: 

Here is some code you might find useful. For many many more options, see Regular-expressions.info.

All code Using System.Text.RegularExpressions;


Remove all occurrences of {mm} (and only mm):

Regex.Replace(myString, "{mm}", String.Empty, RegexOptions.None);

Replace all occurrences of {mm} (and only mm) with mm:

Regex.Replace(myString, "{mm}", "mm", RegexOptions.None);

Remove all occurrences of {any-characters}:

Regex.Replace(myString, @"{[\-a-z]*}", String.Empty, RegexOptions.IgnoreCase);

Replace all occurrences of {any-characters} with any-characters:

Regex.Replace(myString, @"{(?<tag>[\-a-z]*)}", "${tag}", RegexOptions.IgnoreCase);
Daniel Rasmussen
@cyclotis04: Regular expressions are new to me and I think they are not easy to understand. I've visited this site before but I needed fast solution. This is why I ask here. But thanks for the advice ;)Also stackoverflow is like an archive for me. So if I need same thing in future, here I look first :)
HasanGursoy
@HasanGursoy: Yes, there's definitely nothing regular about them. If you find that one of the solutions I gave doesn't do what you want, tell me what went wrong, and I can try and help you out with a more specific Regular Expression. =]
Daniel Rasmussen
@Hasan: I can also recommend http://msdn.microsoft.com/en-us/library/az24scfc.aspx
Joren
@cyclotis04: I'm getting "parsing "{(?[\-a-z]*)}" - Unrecognized grouping construct." error with the last one. Probably using result.Replace("{", String.Empty).Replace("}", String.Empty); will do the same.
HasanGursoy
@Hasan @cyclotis probably meant `{([\-a-z]*)}` (remove the `?`). BTW, the `-` need not be escaped within the character class but be sure to place it at either the beginning or end of the class as shown to prevent unintended character range specifications. Keeping it escaped does no harm of course, just an FYI.
Ahmad Mageed
@Ahmad: Correct - or, you can add a group name, like `tag` (which I did.) For some reason I tried to find a middle ground between naming it and not naming it. =P
Daniel Rasmussen
@Hasan: `result.Replace("{", String.Empty).Replace("}", String.Empty);` would get rid of **ALL** the brackets, which *may* be what you want. If you want to get rid of only some of them, Regular Expressions are the way to go.
Daniel Rasmussen
@cyclotis naming is definitely clearer. Usually if a small number of groups are involved it's not too hard to follow as long as they aren't nested, otherwise naming them is much better. BTW your named group syntax in the replacement string needs to be `${tag}` not `$tag`. For numbered groups it's `$n` but for named groups it's `${group-name}`.
Ahmad Mageed
@Ahmad: You're right, you're right... It's been too long of not working with Regex's... >.<
Daniel Rasmussen
This is how I learn :D. Thank you all!
HasanGursoy
A: 

.Net already provides a similar functionality using String.Format.

string.Format("value {0}", ""); // returns "value "
string.Format("value {0}", "some value"); // returns "value some value"

You can do more advanced things with this method, such as specifying formatting for numbers:

string.Format(new CultureInfo("en-US"), "value {0:N}", 10000); // returns 10,000.00
string.Format(new CultureInfo("es-ES"), "value {0:N}", 10000); // returns 10.000,00

The two downsides to this method:

  1. No case for removing the brackets.
  2. Can't name your placeholders. You have to use ordinal placeholders. {0}, {1}...{n}.
Greg
@Greg: Nice but not exactly what I need. I need to know what to replace with the tag. For example there are tags like {power-source}. I cannot replace all zeros with its language label. Also it's not user friendly.
HasanGursoy
Ah, yeah, it's not good for user-inputted stuff. Gotcha.
Greg