Jump to content
Sign in to follow this  
uteotw

How to use StringRegExpReplace to replace \ / : * ? " < > |

Recommended Posts

uteotw

I wish to replace certains characters from a string by a Space.

That string will be used to create a file name.

Obvioulsy the forbidden characters are: \ / : * ? " < > |

So I want to use StringRegExpReplace

$nametemp = StringRegExp($textline, $RegExpName, 3) ; read temporay file name from line of text

$filename = StringRegExpReplace($nametemp[0], "[\/:*?"<>|]", " ") ; remove forbidden characters

But it doesn't work because \ * ? < > are special RegExp character.

What can I do?

Thanks

uteotw

Share this post


Link to post
Share on other sites
PartyPooper

I used:

If StringRegExp($Filename, "[^ a-zA-Z0-9_\-]") Then
MsgBox(262160, "ERROR", "Filename contains illegal characters")
Endif

Not exactly what you want but you can build on it.

Share this post


Link to post
Share on other sites
PartyPooper

But it doesn't work because \ * ? < > are special RegExp character.

What can I do?

Forgot to add, try adding a backslash first eg.

\\ \* \? \< \>

Share this post


Link to post
Share on other sites
b1t5tR3@m

@uteotw

I know this isn't what you were originally after...

I want to use StringRegExpReplace

BUT this works pretty well:

#include <GuiConstants.au3>

Dim $dcode[1]
_ArrayAdd($dcode, '[')
_ArrayAdd($dcode, '\')
_ArrayAdd($dcode, '/')
_ArrayAdd($dcode, ':')
_ArrayAdd($dcode, '*')
_ArrayAdd($dcode, '?')
_ArrayAdd($dcode, '"')
_ArrayAdd($dcode, '<')
_ArrayAdd($dcode, '>')
_ArrayAdd($dcode, '|')
_ArrayAdd($dcode, ']')

Dim $ecode[1]
_ArrayAdd($ecode, ' ')
_ArrayAdd($ecode, ' ')
_ArrayAdd($ecode, ' ')
_ArrayAdd($ecode, ' ')
_ArrayAdd($ecode, ' ')
_ArrayAdd($ecode, ' ')
_ArrayAdd($ecode, ' ')
_ArrayAdd($ecode, ' ')
_ArrayAdd($ecode, ' ')
_ArrayAdd($ecode, ' ')
_ArrayAdd($ecode, ' ')

;=============GUI Creation

If Not IsDeclared('WS_CLIPSIBLINGS') Then Global $WS_CLIPSIBLINGS = 0x04000000

GuiCreate("Forbidden Character Stripper", 460, 321,(@DesktopWidth-460)/2, (@DesktopHeight-321)/2 , $WS_OVERLAPPEDWINDOW + $WS_VISIBLE + 

$WS_CLIPSIBLINGS)

$Edit_1 = GuiCtrlCreateEdit("", 10, 10, 440, 270)
$Button_2 = GuiCtrlCreateButton("Strip!", 10, 290, 110, 20)
$Button_3 = GuiCtrlCreateButton("Clear", 190, 290, 80, 20)
$Button_5 = GuiCtrlCreateButton("Open File", 280, 290, 80, 20)
$Button_6 = GuiCtrlCreateButton("Save File", 370, 290, 80, 20)

;=============Code Body

GuiSetState()
While 1
    $msg = GuiGetMsg()
    Select
        Case $msg = $Button_2
            Strip()

        Case $msg = $Button_3
            GUICtrlSetData($Edit_1, "")  

        Case $msg = $Button_5
            _TextfromFile2()
        
        Case $msg = $Button_6
            $text = GUICtrlRead($Edit_1)
            $file = FileSaveDialog("Save to where", @ScriptDir, "All files (*.*)", 16)
            FileWrite($file, $text)

    Case $msg = $GUI_EVENT_CLOSE
        ExitLoop
    Case Else
    ;;;
    EndSelect
WEnd
Exit

;================Included Functions

Func Strip()
    $text = GUICtrlRead($Edit_1)
    For $i = 1 To Ubound($ecode) - 1 Step 1
        $text = StringReplace($text, $dcode[$i], $ecode[$i], 0, 1)
    Next
    GUICtrlSetData($Edit_1, $text)
EndFunc


While 1
    Sleep(250)
Wend

;================Included Functions

Func _ArrayAdd(ByRef $avArray, $sValue)
    If IsArray($avArray) Then
        ReDim $avArray[UBound($avArray) + 1]
        $avArray[UBound($avArray) - 1] = $sValue
        SetError(0)
        Return 1
    Else
        SetError(1)
        Return 0
    EndIf
EndFunc  ;==>_ArrayAdd

;================Included Functions

Func _TextfromFile2()

    $file = FileOpenDialog("Open what file?", "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}", "All files (*.*)")
        If Not @error Then
            $chars = FileRead($file, FileGetSize($file))
            GUICtrlSetData($edit_1, $chars)
        EndIf

EndFunc  ;==>_TextfromFile2

I tried it on a couple of examples; that is, if I understand what it is exactly you are after.

Example: What?the"hell[is]going|on? <This>is*another\test/

When I "paste" in the above example, it will remove all special characters and replace them with spaces.

Hope this helps...

Oh, BTW...this was modified by something that AcidicChip first posted (which intrigued me), and I thought, you know I'll bet that could be modified to do what uteotw wants to do...and it worked.

Share this post


Link to post
Share on other sites
Francis Lennert (France)

Hello

This works for the ^ \ & characters.

HTH,

Francis

MsgBox(1," ",StringRegExpReplace("Er\&u\\.doc^","[\\\^&]"," "))
Edited by Francis Lennert (France)

Share this post


Link to post
Share on other sites
CyberSlug

;there is always this low-tech approach:

$filename = StringReplace($filename, "\", " ")

$filename = StringReplace($filename, "*", " ")

$filename = StringReplace($filename, "?", " ")

$filename = StringReplace($filename, "<", " ")

$filename = StringReplace($filename, ">", " ")


Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!

Share this post


Link to post
Share on other sites
Francis Lennert (France)

That's the good one :

MsgBox(1," ",StringRegExpReplace("E*r""\&u?<>.doc^","[\^&:*?<>\\""]"," "))

I have double(d) the "" in the name of the file for AutoIt reasons !!

HTH

Edited by Francis Lennert (France)

Share this post


Link to post
Share on other sites
Francis Lennert (France)

Hello,

Another way to do your job is to change all the characters that you don't authorize ( which are not A...Z, 0..9 , space , . ...)

MsgBox(1," ",StringRegExpReplace("E*r\&u?<>.doc^","[^a-zA-Z0-9. ]",""))

For my point of view, it seems to be a better way and more secured.

HTH,

Francis

Edited by Francis Lennert (France)

Share this post


Link to post
Share on other sites
uteotw

Thanks Francis, your code works perfectly for what I want to do.

Thanks to everyone else too.

uteotw

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.