6

I have a string where I need to replace all hyphens, but only between two delimiters, "clients_field_" and ":"

For example:

"clients_field_4741a6c5-3855-4455-b487-0b38b0038ae6": "info@domain.com",
"clients_field_78f225e0-1a78-4930-b251-ad2217baeb1b": "2017-07-26"

When all hyphens are removed it should look like this:

"clients_field_4741a6c538554455b4870b38b0038ae6": "info@domain.com",
"clients_field_78f225e01a784930b251ad2217baeb1b": "2017-07-26"

I have tried to find a working regular expression, but I need a little help. I tried the expression (?<=clients_field_)(.*)(?=:), but this will of course select everything between "clients_field_" and ":".

Look at my example

If I can get a few lines of C# code it would be amazing! But I think the RegEx Expression will be just fine! :-)

Thank you!

EDIT: Forgive me! Forgot to mention that the example above is part of a larger json string. So a simple replace with mystring.Replace("-", "") will not work.

EDIT2: Updated my example

  • 2
    It looks like the part up-to the colon has the same number of characters. if this is the case then using regex is unnecessary. You can split the string into two parts, and remove the hyphens from the first part. – Andy G Aug 08 '17 at 18:35
  • If your prefix is always `clients_field_` you can just substring and do a replace on the GUID only. Then concatenate with `clients_field_` – maccettura Aug 08 '17 at 18:39
  • Forgive me! Forgot to mention that the example above is part of a larger json string. So simple replace with mystring.Replace("-", "") will not work. Yes @AndyG, they are all the same number of characters, but since it is a large json string, I think RegEx might be better? – Jocke Pihlström Aug 08 '17 at 18:42
  • Try [`(?<=\G(?!^)|clients_field_)([^-:]*)-`](https://regex101.com/r/ywviiC/2) regex based on [`\G` anchor](https://stackoverflow.com/questions/14897949/what-is-the-use-of-g-anchor-in-regex) and replace with `$1` what's captured inside first [capturing group](http://www.regular-expressions.info/brackets.html). – bobble bubble Aug 08 '17 at 18:49
  • Thank you @bobblebubble! That was what I was looking for! – Jocke Pihlström Aug 08 '17 at 19:03
  • You're welcome! I put an answer with a minor modification. Changed `[^-:]*` to `\w*` for zero or more *word characters*. – bobble bubble Aug 08 '17 at 20:22

2 Answers2

2

Try this code:

var input = "clients_field_78f225e0-1a78-4930-b251-ad2217baeb1b: 2017-07-26\n"
    + "clients_field_ce1649d3-18e6-48af-a9fb-871c577c7da6: 2018-12-31";
var regex = new Regex("^(?<const>clients_field_)(?<p1>[^:]+)(?<p2>.+)$", RegexOptions.Multiline);
var lines = regex.Matches(input)
    .Cast<Match>()
    .Select(g => $"{g.Groups["const"].Value}{g.Groups["p1"].Value.Replace("-", "")}{g.Groups["p2"].Value}");
var result = string.Join("\n", lines);

result would be

clients_field_78f225e01a784930b251ad2217baeb1b: 2017-07-26 clients_field_ce1649d318e648afa9fb871c577c7da6: 2018-12-31

Aleks Andreev
  • 7,016
  • 8
  • 29
  • 37
1

Try a regex based on \G anchor for chaining matches to the desired starting point.

(?<=\G(?!^)|clients_field_)(\w*)-

Replace with what was captured inside the first capturing group.

$1
  • (?<= opens the lookbehind
  • \G(?!^) continues matching on last match but not at ^ start or
  • |clients_field_ match position where the substring is behind
  • (\w*)- capture zero or more word characters followed by -

See this demo at regex101

bobble bubble
  • 16,888
  • 3
  • 27
  • 46