0

I have the following string:

"data-template='Test xxx' root{--primary-font:'XYZ Sans';--secondary-font:'Test Sans';--hero-background:#ffbe3f;--header-colour-highlight:#f0591e;--header-background:#ffffff;--header-colour-tabs:#1d2130; }"

I need to replace the spaces from -font:'XYZ Sans' and -font:'Test Sans' in order to make it -font:'XYZSans' and -font:'TestSans'

Edit: the text inside the -font: may change it is not static.

Could anyone help with that?

2
  • Which programming language do you use? Commented Nov 7, 2022 at 16:40
  • If you expect more than two segments separated by spaces, this will probably be easiest in two passes: one to get the value out of the -font variables, and another to replace the blank spaces in it. Otherwise, this should do sed -E "s/-font:'(\S*)\s+(\S*)';/-font:'\1\2';/g" Commented Nov 7, 2022 at 16:55

1 Answer 1

2

Try this:

Edit: This also works (?<=XYZ|Test) (?=Sans).

(?<=XYZ) (?=Sans)|(?<=Test) (?=Sans)

1- (?<=XYZ) (?=Sans) match a space preceded by XYZ but do not include XYZ as a part of that match, at the same time the space should be followed by Sans, but don't include Sans as a part of the match, we only want the space . This part will match the first space between XYZ Sans

2- | the alternation operator | , it is like Boolean OR If the first part of the regex(i.e., the pattern before |) matches a space , the second part of the regex(i.e., the pattern after |) will be ignored, this is not what we want because of that we have to add g modifier which means get all matches and don't return after first match. See live demo. to check the g modifier and try to unset it and see the result. it is the g right after the regex pattern looks like that /(?<=XYZ) (?=Sans)|(?<=Test) (?=Sans)/g <<

3- (?<=Test) (?=Sans) match a space preceded by Test but do not include Test as a part of that match, at the same time the space should be followed by Sans, but don't include Sans as a part of the match, we only want the space. This part will match the second space between Test Sans

EDIT: This is another regex pattern will match any space exists inside the value of -font:, it is dynamic.

(?<=-font:\s*['\x22][^'\x22]*?)\s(?=[^'\x22]*)

See live demo.

"data-template='Test xxx' root{--primary-font:'XYZ Sans';--secondary-font:'Test Sans';--hero-background:#ffbe3f;--header-colour-highlight:#f0591e;--header-background:#ffffff;--header-colour-tabs:#1d2130; }"

The C# code that does what you want is something like this:

Note: I updated the regex pattern in the code.


using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "\"data-template='Test xxx' root{--primary-font:'XYZ Sans';--secondary-font:'Test Sans';--hero-background:#ffbe3f;--header-colour-highlight:#f0591e;--header-background:#ffffff;--header-colour-tabs:#1d2130; }\"";
      string pattern = @"(?<=-font:\s*['\x22][^'\x22]*?)\s(?=[^'\x22]*)";
      string replacement = "";
      string result = Regex.Replace(input, pattern, replacement);
      
      Console.WriteLine("Original String: {0}", input);
      Console.WriteLine("\n\n-----------------\n\n");
      Console.WriteLine("Replacement String: {0}", result);                             
   }
}

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but I forgot to mention that the values inside the -font: they can change
I will update the answer with the new regex pattern, thank you !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.