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

前端 未结 4 1626
情深已故
情深已故 2020-12-08 03:24

In Perl \\S matches any non-whitespace character.

How can I match any non-whitespace character except a backslash \\?

相关标签:
4条回答
  • 2020-12-08 04:01

    You can use a lookahead:

    /(?=\S)[^\\]/
    
    0 讨论(0)
  • 2020-12-08 04:09

    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.

    echo a b cX | sed -r "s/(a\sb[[:space:]]c[^[:space:]])/Result: \1/"
    
    Result: a b cX
    
    • first space I match with \s
    • second space I match alternatively with [[:space:]]
    • the X I match with "all but no space" [^[:space:]]

    These two will not work:

    a[:space:]b  instead use a\sb or a[[:space:]]b
    
    a[^\s]b      instead use a[^[:space:]]b
    
    0 讨论(0)
  • 2020-12-08 04:14

    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
    
    0 讨论(0)
  • 2020-12-08 04:21

    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".

    0 讨论(0)
提交回复
热议问题