regex help

Post Reply
Caled
Posts: 403
Joined: Thu Apr 09, 2009 4:45 am

regex help

Post by Caled »

Alias: ^go(d)?(s|g)?\s?([A-Za-z' ]*?)$

Things I want it to match:

go
god
gos
gods
go one or more words after
god one or more words after
gos one or more words after
gods one or more words after

It matches all of that BUT it also matches"

goone or more words after
godone or more words after
gosone or more words after
godsone or more words after

The space needs to be optional, in case I am not putting the one or more words after, but if the words after it are in fact present, the space must be there.

Can anyone help there? I considered putting the space in the same capture group as the ([A-Za-z' ]*?) but the problem there, is that in the script of the alias I make a list call on the value of that capture group, which will not match if there is a space at the beginning of it.

Caled
Posts: 403
Joined: Thu Apr 09, 2009 4:45 am

Re: regex help

Post by Caled »

Never mind, I found someone to ask, and this seems to be working:

^go(d)?(s|g)?(\s([A-Za-z' ]*))?$

I didn't realise I could put a capture group inside a capture group. Lots of possibilities there.

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: regex help

Post by Heiko »

I suggest a simple solution:

^go$ for your "go only case"
^go (.*) for your "go followed by several words case"
^gos$
^gos (.*)

etc.

Relying too much on complex regex is not a good idea generally speaking unless it cannot be avoided, simply because they might match in cases that you haven't thought of and generally make debugging your system more difficult in the future.

Caled
Posts: 403
Joined: Thu Apr 09, 2009 4:45 am

Re: regex help

Post by Caled »

I would agree, but part of the point to originally doing this, was to give myself a problem to try and solve in regex, to learn more about it. Which I did. Certainly, if it ever causes problems in the future I will just take it back to basics.

Post Reply