0

I have a string :

Patient: ${ss.patient.howard.firstName} ${ss.patient.howard.lastName} Gender: ${ss.patient.howard.sex} Birthdate: ${ss.patient.howard.dob} ${ss.patient.howard.addressLine1} Phone: (801)546-4765 

and I am trying to replace ${..} substrings with other strings so it would look like:

Patient: firstName lastName Gender:sex Birthdate: dob addressLine1 Phone: (801)546-4765 
3
  • I am trying examleString.replaceFirst("\\${(.*?)\\}", (String) TestcaseContext.getCache().get(matcher.group(1))) and getting, Illegal repetition near index 1 \${(.*?)\} Commented Mar 30, 2016 at 19:26
  • may be This will guide you in right direction? Commented Mar 30, 2016 at 19:30
  • Must you use regular expressions? I would consider the Apache Commons Lang StrSubstitutor for this particular application. It does exactly what you want. Commented Mar 30, 2016 at 19:31

2 Answers 2

2

You can use this regex with a capturing group:

String myString = "Patient:\n${ss.patient.howard.firstName} ${ss.patient.howard.lastName}\nGender: ${ss.patient.howard.sex}\nBirthdate: ${ss.patient.howard.dob}\n${ss.patient.howard.addressLine1}\nPhone: (801)546-4765"; myString = myString.replaceAll("\\$\\{[^}]+?\\.([^.}]+)}", "$1"); System.err.println(myString); 

([^.}]+) is the capturing group before } and after the last DOT.

RegEx Demo

Output:

Patient: firstName lastName Gender: sex Birthdate: dob addressLine1 Phone: (801)546-4765 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @anubhava, good to know about the capturing group. It did the trick.
0

You could try searching for ${string.string.string OR } and replace with empty string or nothing like this.

Regex: \${[a-z]+\.[a-z]+\.[a-z]+\.|}

Replacement to do: Replace with nothing or empty string.

Regex101 Demo

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.