Skip to main content

Hi,

Can anyone advise me what regex is required (potentially within StringReplacer) to remove ALL characters in a string EXCEPT

lower case a to z,

upper case A to Z

number sign #

colon :

I want to smash/collapse the entire string together removing everything except occurrences of these 54

characters.

Thanks in advance,

Rob

Desktop (2015.1)

Hi @rob14

I think this will help you. It will select everything except the things that should not be selected so you can replace it with ''

[^(a-z|A-Z|#)]


Hi @jeroenstiers,

Thanks very much, the syntax above nearly does everything (just need to leave the colon in place).

I tweaked your code, which appeared to leave the colon as well; however, I am not sure of this is the 'correct' use of regex [^(a-z|A-Z|#:)]

Should there be an additional vertical bar between the # and : (is the bar technically acting as a logical 'OR' or maybe an 'AND' as it is in fact a multiple negation?) to give me [^(a-z|A-Z|#|:)]

Thanks again,

Rob


[^(a-zA-Z#:)]


because else it would also not select a "|" if you use that.

Anything between square brackets is a character class. So adding a pipeline character is a character and not a pipeline.

In this case it is a negated character class.

and if you want to negate a "-" you would need to add a escaped one.

[^(a-zA-Z#:\\-)]


Hi @gio,

Thanks for the further explanation, regarding the loss of regex 'special character' super powers if included within the class brackets[]. Essentially meaning a 'special character' becomes a 'normal' search criteria (escape pending) (I guess the brackets are to special characters what Kryptonite is to Superman).

I have revised my StringReplacer to [^(a-zA-Z#:)] and all is well.

Thanks again,

Rob


[^(a-zA-Z#:)]

I think the regex excludes the round brackets ( ) too. Probably this regex would exactly match the requirement.

 

p^a-zA-Z#:]

[^(a-zA-Z#:)]

I had to answer in a hurry. Thanks for finding the mistake!

 

 


Reply