Python regex replace string with pattern

时间:2017-12-18 07:32:57

标签: python regex python-2.7

Regex is int variableWhichIncrement= -1; public Integer getIncrementVariable(){ variableWhichIncrement=variableWhichIncrement+1; return variableWhichIncrement; } , This regex will allow alphanumeric + some special chars.

I want to replace characters which are not allowed.

I have tried,

r'[A-z\d,\-.\ \/\n]{1,}'

Gives output as,

re.sub(r'[A-z\d,\-.\ \/\n]{1,}', ' ', 'ASGHB 3 JHDSD eyg && ^&*hdbcd v%^&*B#$%^')

I want original string as output with replaced special chars (which are not allowed) with white spaces.

Expected output : ' && &* % &* #$% ' How to achieve this ?

1 个答案:

答案 0 :(得分:3)

You can find all about re.sub here

So about your question. You should use ^ before your set:

var toString = output.fromBase64() //"[1, 2, 4, 65]"
if let str = toString {
    let chars = CharacterSet(charactersIn: ",][ ")
    let split = str.components(separatedBy: chars).filter { $0 != "" }.flatMap { Int($0)}
    print(split)  //[1, 2, 4, 65]
}