Question

String Replacer - find symbol or letter or number followed by Capital Letter and insert a space

  • 12 November 2021
  • 4 replies
  • 8 views

Badge

I have text where spaces between some words is missing. These spots are easy to find with regex because it is always something following by capital letter without the space. I can't workout how to add the space though.

My String Replacer looks like this: @ReplaceRegEx(@Value(Speciality),([^ ])([A-Z]), \\1, \\2)

 

The output it produces is:

Anxiety, Panic & Phobia shildren & Adolescent selationships Outside work (ndividual counselling )eadership

 

Original:

Anxiety, Panic & PhobiasChildren & AdolescentsRelationships Outside work (Individual counselling)Leadership

 

So it adds space, just not where I need it and removes the capital letter. I tried many variations, but none of them produces the needed outcome.

 


4 replies

Userlevel 1
Badge +21

You just add the space between your matched groups

@ReplaceRegEx(@Value(Speciality),([^ ])([A-Z]),\1 \2)

So the regex matches two groups, and you replace the matched part with the first group followed by a space followed by the second group. You have a space before your first matched group and then the comma thinks it's got to the end so the second matched group is not part of the replacement

 

You could also alter the regex to use a look behind, and then you just need to replace the match with the space and itself

@ReplaceRegEx(@Value(Speciality),(?<=[^ ])([A-Z]), \1)

 

 

 

Badge

You just add the space between your matched groups

@ReplaceRegEx(@Value(Speciality),([^ ])([A-Z]),\1 \2)

So the regex matches two groups, and you replace the matched part with the first group followed by a space followed by the second group. You have a space before your first matched group and then the comma thinks it's got to the end so the second matched group is not part of the replacement

 

You could also alter the regex to use a look behind, and then you just need to replace the match with the space and itself

@ReplaceRegEx(@Value(Speciality),(?<=[^ ])([A-Z]), \1)

 

 

 

Thanks for your reply. it still does not produce what I am after. 

With this expression @ReplaceRegEx(@Value(Speciality),([a-z])([A-Z]),\1 \2): 

From this  text  Anxiety, Panic & PhobiasChildren & AdolescentsRelationships Outside work (Individual counselling)Mediation & Conflict

I now get Anxiety, Panic & Phobias Children & Adolescents Relationships Outside work (Individual counselling)Mediation & Conflict

 

Which is better, but having commas would make so much more sense.  Comma inside replacement text does not work. 

Userlevel 1
Badge +21

Thanks for your reply. it still does not produce what I am after. 

With this expression @ReplaceRegEx(@Value(Speciality),([a-z])([A-Z]),\1 \2): 

From this  text  Anxiety, Panic & PhobiasChildren & AdolescentsRelationships Outside work (Individual counselling)Mediation & Conflict

I now get Anxiety, Panic & Phobias Children & Adolescents Relationships Outside work (Individual counselling)Mediation & Conflict

 

Which is better, but having commas would make so much more sense.  Comma inside replacement text does not work. 

If you want to actually insert a space and a comma then you need to wrap the whole replacement text in quotes

e.g.

@ReplaceRegEx(@Value(Speciality),([^ ])([A-Z]),"\1, \2")

Although you probably want to change your regex to ignore the capital letter after a bracket

Badge

Thanks for your reply. it still does not produce what I am after.

With this expression @ReplaceRegEx(@Value(Speciality),([a-z])([A-Z]),\\1 \\2):

From this text Anxiety, Panic & PhobiasChildren & AdolescentsRelationships Outside work (Individual counselling)Mediation & Conflict

I now get Anxiety, Panic & Phobias Children & Adolescents Relationships Outside work (Individual counselling)Mediation & Conflict

 

Which is better, but having commas would make so much more sense. Comma inside replacement text does not work.

Thank you. This did a perfect job. Appreciate your help!

Reply