Skip to main content
Solved

Regex multiple character replace

  • April 4, 2017
  • 7 replies
  • 325 views

Forum|alt.badge.img

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)

Best answer by gio

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

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

7 replies

Forum|alt.badge.img+7

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|#)]


Forum|alt.badge.img
  • Author
  • April 4, 2017

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


gio
Contributor
Forum|alt.badge.img+15
  • Contributor
  • Best Answer
  • April 4, 2017

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


gio
Contributor
Forum|alt.badge.img+15
  • Contributor
  • April 4, 2017

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#:\\-)]


Forum|alt.badge.img
  • Author
  • April 4, 2017

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


takashi
Celebrity
  • April 5, 2017

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

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

 

[^a-zA-Z#:]

Forum|alt.badge.img+7

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

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