How to match any non white space character except a particular one?

Clash Royale CLAN TAG#URR8PPP
How to match any non white space character except a particular one?
In Perl S matches any non-whitespace character.
S
How can I match any non-whitespace character except a backslash ?
4 Answers
4
You can use a character class:
/[^s\]/
matches anything that is not a whitespace character nor a . Here's another example:
[abc] means "match a, b or c"; [^abc] means "match any character except a, b or c".
[abc]
a
b
c
[^abc]
a
b
c
You can use a lookahead:
/(?=S)[^\]/
It looks ahead if it's not a space. And then the negative class accepts anything (which is not a space) except the characters in your class.
– Denis de Bernardy
May 25 '11 at 14:30
I like this solution. It's good for things like "give me all the non-word characters except whitespace":
/(?=S)W/– jocull
Feb 24 '17 at 19:55
/(?=S)W/
This worked for me using sed [Edit: comment below points out sed doesn't support s]
[^ ]
while
[^s]
didn't
# Delete everything except space and 'g'
echo "ghai ghai" | sed "s/[^sg]//g"
gg
echo "ghai ghai" | sed "s/[^ g]//g"
g g
s matches more than just the space character. It includes TAB, linefeed carriage return, and others (how many others depends on the regex flavor). It's a Perl invention, originally a shorthand for the POSIX character class [:space:], and not supported in sed. Your first regex above should be s/[^[:space:]g]//g.– Alan Moore
Feb 10 '16 at 20:43
s
[:space:]
sed
s/[^[:space:]g]//g
Yup @AlanMoore works:
echo "ghai ghai" | sed "s/[^[:space:]g]//g" Yields: g g– storm_m2138
Feb 10 '16 at 22:20
echo "ghai ghai" | sed "s/[^[:space:]g]//g"
g g
On my system: CentOS 5
I can use s outside of collections but have to use [:space:] inside of collections. In fact I can use [:space:] only inside collections. So to match a single space using this I have to use [[:space:]]
Which is really strange.
s
[:space:]
[:space:]
[[:space:]]
echo a b cX | sed -r "s/(asb[[:space:]]c[^[:space:]])/Result: 1/"
Result: a b cX
s
[[:space:]]
[^[:space:]]
These two will not work:
a[:space:]b instead use asb or a[[:space:]]b
a[^s]b instead use a[^[:space:]]b
As of sed 4.4, it is apparently still true that you have to use
([^[:space:]]) instead of ([^s]). I'm on openSUSE Tumbleweed 2018 04 03.– user2394284
Apr 6 at 11:01
([^[:space:]])
([^s])
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
How does this work?
– Lazer
May 25 '11 at 13:29