Jump to content

Identify single character from list - Completed


shornw
 Share

Recommended Posts

Probably easier to read the code first as my attempts to explain it first got too long winded

What I would like is for each of the characters : \ / ? * [ ] to be tested in the last Case without writing a separate Case for each.

If StringLen($shName) > 30 Then
While 1
  $shName = InputBox("Tab Name", "Please enter a name for the Excel tab", "", "", 250, 60)
  If @error = 1 Then Exit
  Select
   Case StringLen($shName) > 30
    MsgBox(16, "Error", "The name you chose is too long. Names must be 30 characters or less")
    ContinueLoop
   Case StringLen($shName) < 1
    MsgBox(16, "Error", "You must specify a name for the Excel tab")
    ContinueLoop
   Case StringInStr($shName, "\")
    MsgBox(16, "Error", "Names cannot contain any of the characters listed below" &@CRLF & @CRLF & ': \ / ? * [ ]')
    ContinueLoop
  EndSelect
  ExitLoop
WEnd
EndIf

Can anyone think of a way, or am I just being greedy.

Thanks

Edited by shornw

[font='Comic Sans MS']Eagles may soar high but weasels dont get sucked into jet engines[/font]

Link to comment
Share on other sites

I think the pattern would be this. Check and see or post an example string to test..

$test = StringRegExp($string, '[/?*[]]')

edit: nope its this for valid file names..

StringRegExp($sList, "[/:<>|]")

edit2: seen written by guinness :).

edit4000: Just look at stringregexp() in the doc. Brackets allow to match any character in the set. e.g. [aeiou] matches any lower-case vowel

Edited by Beege
Link to comment
Share on other sites

Link to comment
Share on other sites

MikeMan: Haven't tried it but that's the way I would have probably got to eventually, given enough time and patience. Who wasit said 'If you give a room full of monkeys a typewriter each...." :)

Beege & RobJong: I guess the time has come to stop chickening out from using StringRegExp() - Boy, it messes with my head and I've managed to avoid it for years. Time to man up then!!

RobJong: Tempting as (ab)using for an easy out may be, I gotta get to grips with regular expressions. They certainly seem to be very powerful. Well, that's my weekend screwed...

FYI I elected to use

Case StringRegExp($shName, '[\\/:*?"\[\]]')
This being the illegal character set for Excel tabs.

Yay!! I got the leading backslash to add square brackets right first time. Don't laugh!! ;)

Seriously, thank you all for your assistance, very much appreciated

[font='Comic Sans MS']Eagles may soar high but weasels dont get sucked into jet engines[/font]

Link to comment
Share on other sites

Alternatively, try this. It is much simpler, but will do the job perfectly.

stringinstr($shName, "\") or ($shName, "/") or ($shName, "?") or ($shName, "*") or ($shName, "[") or ($shName, "]")

That would fail, but I think you and the op know that. But even the corrected version kinda ugly. It makes 6 function calls that can easily be done with one. Thats essentially what the op is trying to avoid with all the case statements.

If you want it a simple short method and you do not know regular expressions you could (ab)use StringSplit to do it even simpler.

Case Not StringSplit($shName, ":\/?*[]") And Not @error

Clever!
Link to comment
Share on other sites

Yeah the StringSplit is a bit of a hack but gets the job done and is easy to use for people who want to avoid SRE.

If you want to start with regular expressions be sure to check out this website (I recommend you start here with the quickstart), all of it if you can. Learning regex will take you alot longer then a weekend though, trust me :)

There are of course other good resources out there but I suggest you start with that one, it has simple and a bit more advanced examples, but there almost all quite practical and well explained.

Good luck and have fun with it.

Edit: grammar

Edit: link to quickstart

Edited by Robjong
Link to comment
Share on other sites

I also recommend the *** The PCRE (Regular Expression) ToolKit for AutoIT by Geosoft. Makes it very easy for quick expression testing.

Nice link Rob :)

Edited by Beege
Link to comment
Share on other sites

I was actually looking for that toolkit link, had seen it around but couldn't remember who it was by. Will have to check that out :)

@shornw you almost got the escaping right, you do not need to escape the left bracket [ as its already in a character set. (you will get it later if you don't now)

so this will do:

StringRegExp($shName, '[/:*?"[]]')

btw, if it's for a filename, on Windows these are the disallowed characters I get presented if attempt to rename a file and type one of these characters; / : * ? " [ ] ]

Edit: stupid smileys ;)

Edited by Robjong
Link to comment
Share on other sites

@shornw you almost got the escaping right, you do not need to escape the left bracket [ as its already in a character set. (you will get it later if you don't now)

Much, much later I think.

BTW, the character set I'm using is the illegal set for use on an Excel workbook tab, not filename (also 32 = max characters for tab)

I was giving up my weekend just to preparing to get to grips with SRE. I suspect I will be worm food before I ever got to learn all of it, and I KNOW I'll have lost the will to live before I understand one half

[font='Comic Sans MS']Eagles may soar high but weasels dont get sucked into jet engines[/font]

Link to comment
Share on other sites

  • Moderators

shornw,

I found this site very useful when I started with SREs - and I still use it regularly. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

If you want to start with regular expressions be sure to check out this website, all of it if you can. Learning regex will take you alot longer then a weekend though, trust me :)

I found this site very useful when I started with SREs - and I still use it regularly. ;)

Is there an echo in here? ;)

Edited by Beege
Link to comment
Share on other sites

  • Moderators

Beege,

Obviously! :)

But it at least shows that it the site is a good one to start learning about SREs. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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