Page 1 of 1

Regex help.

Posted: Tue Jul 20, 2010 1:51 am
by Artorius
Shrine of Eliatha [ne].
I thought that this would be the right regex to capture this information, but its not working...
Code: [show] | [select all] lua
^(\w+) \[(\w+)\].$
So, I'm not sure what I'm doing wrong here.

Artty

Re: Regex help.

Posted: Tue Jul 20, 2010 1:55 am
by WillFa
\w is any letter (any word character). It doesn't match spaces.
^(.*?) \[(.*?)\]$

is what you want.

Re: Regex help.

Posted: Tue Jul 20, 2010 2:06 am
by Vadi
WillFa's solution will work, here is also another one more original to yours:

^(.+) \[(\w+)\].$

Re: Regex help.

Posted: Tue Jul 20, 2010 2:09 am
by Rakon
I would use the following match:

Code: Select all

^(.*?)\s+\[([A-z]+)\]\.$
http://www.strfriend.com/vis?re=^(.*%3F)\s%2B\[([A-z]%2B)\]\.$%0D%0A

Versus:

Code: Select all

^(.*?) \[(.*?)\]$
http://www.strfriend.com/vis?re=^(.*%3F)+\[(.*%3F)\]$%0D%0A

Re: Regex help.

Posted: Tue Jul 20, 2010 2:11 am
by Artorius
Wow, thanks for all the quick replies.

Re: Regex help.

Posted: Tue Jul 20, 2010 2:17 am
by Vadi
Now that is a great website.

Re: Regex help.

Posted: Tue Jul 20, 2010 2:20 am
by Rakon
Vadi wrote:Now that is a great website.
Heh, that is a VERY handy website for visuallising RegEx. I also use http://regexpal.com/ for testing.

(Typically, I check the ^$ match at line breaks box) for testing these triggers.

Very handy.

Re: Regex help.

Posted: Tue Jul 20, 2010 2:52 am
by robg
WillFa wrote:\w is any letter (any word character). It doesn't match spaces.
^(.*?) \[(.*?)\]$

is what you want.
I'm assuming you're trying to match a room short desc + exits.

Of the posted responses, this is the best one. It backtracks less, and unlike the others, will actually not break if there's more than one exit. I can't speak to the mud you play, but the one I play has some occasionally bizarre exits that [a-z,]+ wouldn't match, so better to be safe than sorry. Although to be fair, I'd probably make the [] section optional so that it can match if there are no obvious exits as well, and use some kind of ansi match to recognize that it's a room (server dependant).

Re: Regex help.

Posted: Tue Jul 20, 2010 3:04 am
by Rakon
robg wrote:
WillFa wrote:\w is any letter (any word character). It doesn't match spaces.
^(.*?) \[(.*?)\]$

is what you want.
I'm assuming you're trying to match a room short desc + exits.

Of the posted responses, this is the best one. It backtracks less, and unlike the others, will actually not break if there's more than one exit. I can't speak to the mud you play, but the one I play has some occasionally bizarre exits that [a-z,]+ wouldn't match, so better to be safe than sorry. Although to be fair, I'd probably make the [] section optional so that it can match if there are no obvious exits as well, and use some kind of ansi match to recognize that it's a room (server dependant).
There could very well be more information within the [ ]'s , however the OP simply asked for a RegEx that would match on the line given. It states nothing more about requiring capture or parsing of additional text, or tests.

Ask, and you shall receive!

Re: Regex help.

Posted: Tue Jul 20, 2010 4:18 am
by WillFa
Rakon wrote:
robg wrote:
WillFa wrote:\w is any letter (any word character). It doesn't match spaces.
^(.*?) \[(.*?)\]$

is what you want.
I'm assuming you're trying to match a room short desc + exits.

Of the posted responses, this is the best one. It backtracks less, and unlike the others, will actually not break if there's more than one exit. I can't speak to the mud you play, but the one I play has some occasionally bizarre exits that [a-z,]+ wouldn't match, so better to be safe than sorry. Although to be fair, I'd probably make the [] section optional so that it can match if there are no obvious exits as well, and use some kind of ansi match to recognize that it's a room (server dependant).
There could very well be more information within the [ ]'s , however the OP simply asked for a RegEx that would match on the line given. It states nothing more about requiring capture or parsing of additional text, or tests.

Ask, and you shall receive!
You will do fine in life if you listen to what people say.
You will do great if you listen to what they mean.