Say I want to match a "word" character (\w
), but exclude "_", or match a whitespace character (\s
), but exclude "\t". How can I do this?
How can I exclude some characters from a class?
Asked Answered
Use a negated class including \W or \S.
/[^\W_]/ # anything that's not a non-word character and not _
/[^\S\t]/ # anything that's not a non-space character and not \t
+1 I call this technique a double-negative. –
Unpeople
@ThomasGuyot-Sionnest doing plus is easy;
[/[:graph:]]
. but /
is already in graph. maybe you meant minus the / character? [^/[:^graph:]]
may work for that; not sure what regex engine git is using –
Speller @ysh TBH I can't even make sense of what I wrote above - it wasn't that easy obviously but I don't exactly remember what I was struggling with. I will delete that comment. I remember I found a solution though, will have to look back at what I did... IIRC it used the
cntrl
class. –
Kano I finally checked... my use case was doing a git word diff where I could see just individual path component changes (to compare two build logs with slight path modification), and I'm not sure what I tried but it's very possible I wasn't even trying the right thing at first. It appears the default regex for
--word-diff
is [^[:space:]]+
(I see no diff using it vs plain --word-diff
) and to split on paths too I just added /
, so git diff '--word-diff-regex=[^[:space:]/]+' [...]
. –
Kano Close, but not cigar. \W excludes characters I need included. –
Adenosine
@Adenosine what do you need? I can help –
Speller
@Speller problem regex. I needed it to match the last
},
so I could replace that with \n},
. I have since done this manually, and it was annoying. –
Adenosine @Adenosine I can't get from that what you are trying to do; you want to match balancing {} maybe? what programming language is this? –
Speller
@Adenosine if you are looking for }, at the end of a line that doesn't start with {,
^\s+[^{\s].*},$
or use the possessive modifier ^\s++[^{].*},$
–
Speller © 2022 - 2024 — McMap. All rights reserved.
[a-z-[aeiou]]
in .NET matches a lowercase consonant. – Screak