Regex help.

Post Reply
Artorius
Posts: 7
Joined: Fri Jul 09, 2010 11:29 am

Regex help.

Post 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

WillFa
Posts: 19
Joined: Mon May 10, 2010 4:16 pm

Re: Regex help.

Post by WillFa »

\w is any letter (any word character). It doesn't match spaces.
^(.*?) \[(.*?)\]$

is what you want.

User avatar
Vadi
Posts: 5041
Joined: Sat Mar 14, 2009 3:13 pm

Re: Regex help.

Post by Vadi »

WillFa's solution will work, here is also another one more original to yours:

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

User avatar
Rakon
Posts: 350
Joined: Tue Feb 16, 2010 7:41 pm
Contact:

Re: Regex help.

Post 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
Last edited by Rakon on Tue Jul 20, 2010 3:04 am, edited 1 time in total.

Artorius
Posts: 7
Joined: Fri Jul 09, 2010 11:29 am

Re: Regex help.

Post by Artorius »

Wow, thanks for all the quick replies.

User avatar
Vadi
Posts: 5041
Joined: Sat Mar 14, 2009 3:13 pm

Re: Regex help.

Post by Vadi »

Now that is a great website.

User avatar
Rakon
Posts: 350
Joined: Tue Feb 16, 2010 7:41 pm
Contact:

Re: Regex help.

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

robg
Posts: 7
Joined: Tue Apr 06, 2010 2:07 pm

Re: Regex help.

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

User avatar
Rakon
Posts: 350
Joined: Tue Feb 16, 2010 7:41 pm
Contact:

Re: Regex help.

Post 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!

WillFa
Posts: 19
Joined: Mon May 10, 2010 4:16 pm

Re: Regex help.

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

Post Reply