1

I need a Regex pattern in salesforce to replace all special characters in a string except those between quotes like..

input: hai@#$welcome to 'World@#$'
output: hai welcome to 'World@#$'

I tried the following, but it's not working as expected:

'[^\\w((?<=\')(.*)(?=\'))]'
ruakh
  • 175,680
  • 26
  • 273
  • 307
Manoj Chandran
  • 139
  • 2
  • 7

3 Answers3

3

Your search regex should be:

[^\w\s'](?=(([^']*'){2})*[^']*$)

And replace with:

" "

Online Demo: http://regex101.com/r/lC6zG5

Explanation:

enter image description here enter image description here

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • you might want to include that `\\'` inside the first square bracket – aelor Mar 19 '14 at 06:56
  • It Repalcing the last quote World@#$' but i dont want to replace that I want as it is with the quotes like 'World@#$' – Manoj Chandran Mar 19 '14 at 06:59
  • @ManojChandran: Glad to know, can you mark the answer as accepted by clicking on tick mark on top-left of my answer. – anubhava Mar 19 '14 at 07:14
  • @anubhava can you tell what is that quatifier `{2}` actually counting, I could not understand from the explanation at regex101 – aelor Mar 19 '14 at 07:36
  • 1
    That is to make sure that even number if quotes are followed to avoid quoted strings. – anubhava Mar 19 '14 at 07:44
  • @aelor I got an issue with this regex which occured during testing when i use single ' mark i.e is odd number like hai @!'world it should actually replace that ' mark and @ ! but it s not replacing anything ..Only the string between '' should not be replaced. – Manoj Chandran Mar 24 '14 at 13:18
  • @ManojChandran: I am surprised that you chose not to accept my answer and accepted the answer which was copied from mine. – anubhava Mar 24 '14 at 13:21
  • @Anubhava actually i tried the answer of aeolr first n marked it as answer later only i saw that both answers are same...But it doesnt mean your wrong i understood more from your explanation butI thanked you both – Manoj Chandran Mar 24 '14 at 13:33
  • Usual Stackoverflow protocol is if you have more than correct answer than choose the one that was posted earlier. My answer was posted 17 min before and moreover other answer was mere a copy of my answer. – anubhava Mar 24 '14 at 13:37
  • Ya i accept you ..No hard feeling im just a fresher to everything – Manoj Chandran Mar 24 '14 at 13:38
  • Can you help me out in this again?? – Manoj Chandran Mar 24 '14 at 13:40
  • I can sure help. Can you explain where exactly is the problem? – anubhava Mar 24 '14 at 13:43
  • Actually my requirement was to replace all special characters except those between ' ' quotation but when I use single ' mark it should be replaced ..like hai@!'welcome'World' n output should be Hai welcome'World' – Manoj Chandran Mar 24 '14 at 13:47
  • This regex works an assumption that quotes are balanced but in `hai@!'welcome'World'` you have 3 quotes. – anubhava Mar 24 '14 at 14:00
  • Ya in that case it will be difficult to remove that but what if the input is like Hai@!'?? – Manoj Chandran Mar 24 '14 at 14:08
  • IMO `Hai@!'??` is invalid input for this question since question is all about skipping characters **between the quotes**. Between needs 2 quotes (even number of quotes) – anubhava Mar 24 '14 at 14:09
  • Ya but only those between quotes sometyms i may get input like this also – Manoj Chandran Mar 24 '14 at 14:12
  • But that makes it a totally different problem one that is very difficult to solve using regex. I suggest you create a new question with that problem with all these examples so that you get help from whole community. – anubhava Mar 24 '14 at 14:19
1

use this [^\w\s\'](?=(([^']*'){2})*[^']*$)

same as Anubhavas answer, just a minor change to exclude the '

demo here : http://regex101.com/r/iL7oH7

aelor
  • 10,892
  • 3
  • 32
  • 48
0

manoj, resurrecting this question because there's one tidy solution that wasn't mentioned. This problem is a classic case of the technique explained in this question to "regex-match a pattern, excluding..."

We can solve it with a beautifully-simple regex:

'[^']*'|([^\w'\s]+)

The left side of the alternation | matches complete quoted strings. We will ignore these matches. The right side matches and captures special characters to Group 1, and we know they are the right ones because they were not matched by the expression on the left.

All that's left to do is to replace the match with an empty string, but only when Group 1 is set. The way do to this is different in every language, but it's not complex. The question and article below fully explain the technique (the article has code samples).

On this demo, if you look in the right pane, you can see how Group 1 only captures the chars you want to replace.

Reference

Community
  • 1
  • 1
zx81
  • 41,100
  • 9
  • 89
  • 105