0

I play a mud using mudlet and wish to use Perl regex to capture some input from my prompt. The thing is it seems really hard to do without being overly repetitive in my search. I'm hoping to come up with an elegant solution to

Here is my sample prompt line: [NESWDNeSeSwNw] [The Palace Square-Bastion] [|Excl] >)|61|(<

What I want to capture is the list of directions where each direction begins with an uppercase letter and may or may not contain a lower case letter (in case of diagonals only). Valid upper letters are N E S W U D (cardinal indicators) and the valid lowercase letters are e w only (diagonal indicators)

I was trying to use something to the effect of: ^.([NESWUD]{1}[ew]?)+. (dots to match the brackets since brackets seem to be incapable of being escaped in the pcre that mudlet uses for triggers)

The problem with this solution is that it does not capture all the directions and the greedy matching matches the last directon "Nw" only. If I break it down to remove the capture group and the greediness to just [NESWUD]{1}[ew]? it gives me the every occurance of the capital letters everywhere not just in my prompt.

Any help would be greatly appreciated.

4

2 回答 2

0
([NUESWUD][ew]?)(?=[NSEWUDew\]])

NESWDNeSeSwNw将从[NESWDNeSeSwNw] [The Palace Square-Bastion] [|Excl] >)|61|(<

最后的前瞻(?=[NSEWUDew\]])确保它后面只有指定的字符,这意味着它不会捕获SinSquareEin Excl

在这里尝试测试http://gskinner.com/RegExr/

于 2013-11-14T10:41:38.090 回答
0

我假设像Weor这样的方向Dw没有意义,e并且w只能在Nor之后出现S。你想要的是类似的东西^\[((?:[EWUD]|[NS][ew]?)+)\]。这将捕获 中的所有方向$1,而不仅仅是最后一个。

为了解释它,我将使用一个更简单的正则表达式:((?:[a-z])+). ([a-z])+将匹配任何小写拉丁字母字符串,但每次捕获都会覆盖前一个,只保留最后一个字母(例如Hello将 return o)。((?:[a-z])+)将匹配并捕获相同的字符串(例如Hello将返回ello$1)。(?:[a-z])防止匹配被捕获,+将匹配增加到尽可能多的外部()然后将该字符串捕获到$1.

你不需要{1}在你的正则表达式中,因为这总是暗示的。

于 2013-11-28T06:16:55.397 回答