Regex - Replacing symbol if between two characters/if space after

Hi there,

Please could I ask for some advice on how to replace certain bits of my string variable, dependent on what follows a certain character?

E.g

String variable #1 = Pushin’ N Pullin’
String Variable #2 = Should’ve Been Me

For Pushin’ N Pullin’, I want it to replace the character ’ with nothing, simply remove it - because there is no character immediately after it. However, for Should’ve Been Me, I want to replace the character ’ with a space, because there is a character either side of it.

The rule I want to employ is: if there’s a character either side of a ’ then remove it and replace with a space, and if there is no character after a ’ then just remove it.

Does anyone know how best to write this using Regex?

Thanks in advance!
Sam

Hi @sam.willson

Use it as follows

System.Text.RegularExpressions.Regex.Replace(“Yourstring”,“(?:\s’\s)”," “).Replace(”'“,” ")

cheers

Hi @sam.willson,

You could try the following in an assign:

BoolVar = System.Text.RegularExpressions.Regex.IsMatch(StringVar, "'\w")

This will return True if the string contains an apostrophe and then a letter/number:
image

You can then do an If statement:

Edit: Expressions for Replace on either side:

Apostrophe with space: StringVar = StringVar.Replace("'"," ")
Apostrophe with nothing: StringVar = StringVar.Replace("'","")

Hi,

Can you try the following expression?

yourString = System.Text.RegularExpressions.Regex.Replace(yourString,"’(?=\s|$)","").Replace("’"," ")

Sample20221116-1.zip (2.3 KB)

Regards,

1 Like

Hi @Yoichi - thank you for this, this has worked perfectly! Really appreciate your help.

1 Like

Hi @Anil_G thanks for this, this has worked great! All the best!

Hi @william.coulson cheers for this - I’ve ended up using the solutions posted below, but thanks for this suggestion, will definitely bear it in mind for when I need to incorporate some IFs potentially further down the line. Thanks for your help!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.