Jump to content

how to clean a string from unwanted chars?


Recommended Posts

hello to all
how can I clean a string from unwanted chars and keep only chars belonging in the range from asc(32) to asc(127) inclusive and replace all other chars that falls outside that range with another char (a space chr(32) for example) in one shot, using a regular expression? (or whatever else but looping...)
example the string:

"Hello ☺ «everybody» Love ♥ and peace"

should result as

"Hello   everybody  Love   and peace"


thanks  on advance for helping

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

Try this --

#include <MsgBoxConstants.au3>

Test1()

; This example demonstrates a basic replacement.  It replaces the vowels aeiou
; with the @ character.
Func Test1()
    Local $sInput = "Hello ☺ «everybody» Love ♥ and peace"
    Local $sOutput = StringRegExpReplace($sInput, "[^\x00-\x7F]+", " ")
    Display($sInput, $sOutput)
EndFunc   ;==>Test1

Func Display($sInput, $sOutput)
    ; Format the output.
    Local $sMsg = StringFormat("Input:\t%s\n\nOutput:\t%s", $sInput, $sOutput)
    MsgBox($MB_SYSTEMMODAL, "Results", $sMsg)
EndFunc   ;==>Display

 

Link to comment
Share on other sites

@Danp2, hemmm, sorry, I talked too quickly, that pattern has issues, it doesn't remoove chars below chr(32) and above chr(127)...

try this to see the issue:

#include <MsgBoxConstants.au3>

Test1()

; This example demonstrates a basic replacement.  It replaces the vowels aeiou
; with the @ character.
Func Test1()
    Local $sInput = "Hello ☺ " & @CR & "«everybody» Love ♥ " & ChrW(512) & " and peace"
    Local $sOutput = StringRegExpReplace($sInput, "[^\x00-\x7F]+", " ")
    Display($sInput, $sOutput)
EndFunc   ;==>Test1

Func Display($sInput, $sOutput)
    ; Format the output.
    Local $sMsg = StringFormat("Input:\t%s\n\nOutput:\t%s", $sInput, $sOutput)
    MsgBox($MB_SYSTEMMODAL, "Results", $sMsg)
EndFunc   ;==>Display

 

Edited by Chimp
it's ok for chars above 127

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...