Snehashish
Snehashish

Reputation: 74

Replace whitespaces within a pattern

I have variables(eg. $!{Full Name}, $!{Full Na me}) in a mail template(HTML content). Let's say. A mail template's content is: "Buyer name is $!{Full Name}. Age is 29." Before system stores a mail template, it needs to remove if any whitespaces within the variable. So, the output template will be "Buyer name is $!{FullName}. Age is 29."

For any below inputs the required output is Buyer name is $!{FullName}. Age is 29.

  1. Buyer name is $!{Full Name}. Age is 29.
  2. Buyer name is $!{Full Na me}. Age is 29.
  3. Buyer name is $!{ Full Na me}. Age is 29.

Tried with: \$\!\{((?:\s*[a-zA-Z0-9_]*)*)\} by replacing with $!{$1}

Since I need to implement this in Java, I can parse through the entire content and put these variables (eg. $!{Full Name}, $!{Full Na me}) in a list. Then after removing whitespaces, again put them back? Probably a solution. But, any alternative solution for this?

Upvotes: 2

Views: 84

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626861

In Java, the simplest solution is to match $!{...} strings and remove all whitespace inside them within a call to Matcher#appendReplacement:

String s = "Buyer name is $!{ Full Na me}. Age is 29.";
StringBuffer result = new StringBuffer();
Matcher m = Pattern.compile("\\$!(\\{[^}]+})").matcher(s);
while (m.find()) {
    m.appendReplacement(result, "\\$!" + m.group(1).replaceAll("\\s+", ""));
}
m.appendTail(result);
System.out.println(result.toString());
// => Buyer name is $!{FullName}. Age is 29.

See the Java demo.

The \$!(\{[^}]+}) pattern matches the $! literally, then captures into Group 1 a {, followed with 1+ chars other than }, and then a }. Inside the while block, the whitespaces are removed with .replaceAll("\\s+", "").

If you are a fan of a one-regex solution, you may use

(\G(?!^)|\$!\{)([^}\s]*)\s+

and replace with $1$2, see this regex demo.

See the Java demo:

String s = "Buyer name is $!{ Full Na me}. Age is 29.";
s = s.replaceAll("(\\G(?!^)|\\$!\\{)([^}\\s]*)\\s+", "$1$2");
System.out.println(s);
// => Buyer name is $!{FullName}. Age is 29.

The regex matches

  • (\G(?!^)|\$!\{) - Group 1 (this part will be kept with the $1 backreference): end of the previous match (\G(?!^)) or the $!{ substring
  • ([^}\s]*) - Group 2 (this part will be kept with the $2 backreference): any 0+ chars other than } and whitespace
  • \s+ - 1+ whitespaces (that will be removed).

Upvotes: 2

Amey Dahale
Amey Dahale

Reputation: 750

You can do this in two steps.

  1. Get the substring between delimiters({ and }) -

    (?<=\{)(.*)(?=\})
    
    string : "Buyer name is $!{Full Name}. Age is 29."
    => subString = "Full Name"
    string : "Buyer name is $!{Full Na me}. Age is 29."
    => subString = "Full Na me"
    string : "Buyer name is $!{ Full Na me}. Age is 29."
    => subString = " Full Na me"
    
  2. Now replace the whitespaces in captured substring using-

    /\s/g
    
  3. Merge the original string and the rectified substring.

Upvotes: 0

Related Questions