Jump to content

RegEXP()


Recommended Posts

I want to check whether the string fits a given regular expression pattern : Like a file name *.abc, one line.

Example acice.abc is valid, sncin13r.abc is valid, too.

acimea.jpg is not valid and neither is einvi;.abc

Edited by nht3004

for(loop=0; loop<infinity; loop++) { alert('I love you'); }

Link to comment
Share on other sites

What do you have so far? You can start with this: "\w+.abc"

Depends on what other conditions that need to be met...

But I need a path, so it must allow the "\" character!

Can I use this ""[\\\w]+.abc"?

Edited by nht3004

for(loop=0; loop<infinity; loop++) { alert('I love you'); }

Link to comment
Share on other sites

I want to check whether the string fits a given regular expression pattern : Like a file name *.abc, one line.

Example acice.abc is valid, sncin13r.abc is valid, too.

acimea.jpg is not valid and neither is einvi;.abc

It appears a semi-colon (;) is a valid character for a file name. Although, it is included in the regular expression as one of the characters not to be in the file name.

A one liner.

;
#cs
    THIS LIST IS OUTDATED
    From http://msdn.microsoft.com/en-us/library/6f69bkd1(VS.80).aspx
    File names cannot contain the following characters:
    * pound (#)
    * percent (%)
    * ampersand (&)
    * asterisk (*)
    * vertical bar (|)
    * backslash (\)
    * colon (:)
    * double quotation mark (")
    * less than (<)
    * greater than (>)
    * period (.)
    * question mark (?)
    * forward slash (/)
    * leading or trailing spaces (' ')
    * names reserved for Windows or DOS (nul, aux, con, com1, lpt1, and so on)
#ce

#include <Misc.au3>

Local $sStr = "d:\Program Files\sncin13r.abc" ; " & '"' & "

;Local $sFilName = StringRegExpReplace($sStr, "^.*\\", "")
;ConsoleWrite("$sFilName = " & $sFilName & @CRLF)

Local $Res = StringRegExp(StringRegExpReplace($sStr, "^.*\\", ""),"[/\\:*\?\t\x22<>\|]")

MsgBox(0, "Result", '"' & $sStr & '" is ' & _Iif($Res = 0, "valid file name", " an invalid file name"))
;

Edit: Replaced single quote with space in RE to disallow leading space in file name.

Edit: To comply with GEOSoft's recommendations in Post #9, the regular expression,

"^([^ ][^#%&*|\\:<>.?/;" & Chr(34) & Chr(9) & Chr(10) & Chr(13) & "]+)(\.abc)$"

is changed to

"[/\\:*\?\t\x22<>\|]"

Edited by Malkey
Link to comment
Share on other sites

@Malkey

That list is a bit outdated. The period(.) is valid in a file name. We refer to it as a double extension.

$hFile = @DeskTopDir & "\test.au3.txt"
FileWriteLine($hFile, "As you can see it does work.")
Shellexecute($hFile)

I remember running into this problem when I was parsing filenames in another script.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

@Malkey

That list is a bit outdated. The period(.) is valid in a file name. We refer to it as a double extension.

$hFile = @DeskTopDir & "\test.au3.txt"
FileWriteLine($hFile, "As you can see it does work.")
Shellexecute($hFile)

I remember running into this problem when I was parsing filenames in another script.

As have I. A period is also valid in a folder name. What this implies is that you cannot test to see if a string is a file or folder name simply by looking at it. You have to open it and look at its attributes. I've observed several scripts looking for an extension in a name and then "assuming" it's a file....
Link to comment
Share on other sites

As have I. A period is also valid in a folder name. What this implies is that you cannot test to see if a string is a file or folder name simply by looking at it. You have to open it and look at its attributes. I've observed several scripts looking for an extension in a name and then "assuming" it's a file....

Also true. I had forgotten about that one. And for the non-beleivers we have

$File = @DeskTopDir & "\test.txt\test.au3.txt"
$hFile = FileOpen($File, 10)
FileWriteLine($hFile, "As you can see it does work.")
FileClose($hFile)
Shellexecute($File)

Now back to our regularly scheduled programing.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

another one that is missing from that list is Tab. @Tab or in a RegExp \t

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

One more update. That list is so far out it's easier to create a new list of illegal characters. #% and maybe more are now allowed as is a space before ".abc" Leading spaces are just stripped by Windows.

If StringRegExp($sFile, "[/\\:*\?\t\x22<>\|]") Then MsgBox(0, "Oooops", "Illegal Character used")

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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...