4

Consider I have a string in quotations "( the" which occures in a huge body of text.

I want to remove the space between non-alphanumeric ( and aplphanumeric t characters

Ωmega
  • 42,614
  • 34
  • 134
  • 203
Coddy
  • 881
  • 4
  • 10
  • 16

3 Answers3

6

Replace match of regular expression pattern (?<=\()\s+(?=t) with empty string.

Regular expression visualization

If any alphanumeric character may occur after such space, then use pattern (?<=\()\s+(?=[^\W_])

Regular expression visualization

Ωmega
  • 42,614
  • 34
  • 134
  • 203
  • 1
    It does exactly what I wanted. Namely, it matches only the space between the characters excluding the characters. – Coddy Nov 30 '13 at 19:20
  • @Coddy - `(?<=...)` is lookbehind, `(?=...)` is lookahead. I have updated my answer with diagrams, that should help you to understand it a little bit better. – Ωmega Dec 01 '13 at 01:13
1

Per this answer if you are using version 6.0 or later you can use Perl compatible regular expressions. So you could do the following regex search and replace.

replace (\W) (\w) with \1\2
replace (\w) (\W) with \1\2

This will remove the space between any non-alphanumeric and alphnumeric characters first, then the reverse (alnum, space, non-alnum).

Community
  • 1
  • 1
thesquaregroot
  • 1,414
  • 1
  • 21
  • 35
  • It does match the space but the match includes the characters too that surround the space. It should only match the space. – Coddy Nov 30 '13 at 19:22
  • But when you replace it with \1\2 you are putting back the surrounding characters, effectively removing only the space. – thesquaregroot Nov 30 '13 at 22:32
0

Following regex find/replace will do it for all, not only ( t occurrences:

Find: \( ([a-zA-Z])

Replace: \(\1

Remember to check Regular expression on the bottom of the dialog.

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
  • It does match the space but the match includes the characters too that surround the space. It should only match the space. – Coddy Nov 30 '13 at 19:28