Regular Expression to replace non alpha characters with spaces

前端 未结 2 1888
栀梦
栀梦 2021-01-22 13:32

I have been trying to build a regular expression but haven\'t been able to get one specific condition to work.

I want a regex to remove all non alpha characters with the

2条回答
  •  无人共我
    2021-01-22 13:49

    Here is what I came up with (\s-|[^A-Za-z0-9-])... It will remove all non alphanumerics but keep the "-" except if there is a space before it " -"

    Test using sed in Linux, at the moment I don't have access to VS or Mono to test in C#

    echo "TEST-TEST -TEST#TEST.TEST -1234" | sed 's/\(\s-\|[^A-Za-z0-9-]\)/ /g'
    

    Output

    TEST-TEST TEST TEST TEST 1234
    
    • () and | are used for the OR condition
    • We first remove all " -" using \s-
    • next we keep all alphanumerics and "-" with [^A-Za-z0-9-]

提交回复
热议问题