Jump to content
Sign in to follow this  
Chimp

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


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

Share this post


Link to post
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

 

Share this post


Link to post
Share on other sites

👌It seems to work well
Thanks a lot Dan :)


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

Share this post


Link to post
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

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

Share this post


Link to post
Share on other sites

ah yes, sorry, it was so easy to correct it :> ... I was almost near to correct it myself too. Thanks again

(damn regexp :censored:)


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

Share this post


Link to post
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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...