Jump to content

Help ... String replacement with Regex

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
nikesh

nikesh

    Newbie

  • Members
  • Pip
  • 1 posts
Hi All,

I have a string in following format

abc;h=v8/35bf/3/0/*/k;

In above string I need to replace everything that comes between two ";" (semi colon), moreover the length between these two ";" can vary

I need to replace the above string with...

abc;junk;

Thanks
Nikesh

#2
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts
I never use Regex for replacment, just parseing. However, the lookup query will be ;*.; which will grab everything between the semicolens.

#3
millepag

millepag

    Newbie

  • Members
  • Pip
  • 2 posts
I agree with TkTech, I wouldn't use regex to do what you want to do. I would do something like this:

private string replace(string value)

{

    string[] valueSplit = value.Split(';');

    valueSplit[1] = "junk";

    return String.Join(";", valueSplit);

}

Anyway, with regex you would do it something like this:
private string replace(string value)

{

    System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(";.*;");

    return regex.Replace(value, ";junk;");

}