Regular expressions: Simple examples?

Doc requests, organization, and submissions

Moderators: dorpond, trevor, Azhrei

Post Reply
User avatar
Daniel
Giant
Posts: 132
Joined: Thu Nov 20, 2008 3:49 am
Location: Germany (GMT +1)

Regular expressions: Simple examples?

Post by Daniel »

Hey all. I know RegExes are incredibly powerful, and there's whole books on them, but for MapTool purposes, I think soem really really simple examples would go a long way towards making strfind usable.

For example: You've got a list of token names (possible targets). Before presenting it to the macro user, you want to filter out nonsensical targets like lib:tokens and image:tokens. How would I get the following code snippet to work? It's supposed to filter out all tokens, whose names start with "image:" or "lib:".

Code: Select all

[H,FOREACH(tempVictim, VictimList), CODE:{
   [deleteCondition = if(getGroupCount(strfind(tempVictim, "lib:") ) > 0  ,1,0)]
   [H, IF(deleteCondition == 1): VictimList = listDelete(VictimList, listFind(VictimList, tempVictim))]
}]
"This web of time [...] embraces every possibility. We do not exist in most of them. In some you exist and not I, while in others I do, and you do not. [...] In yet another, I say these very same words, but am an error, a phantom."

J.L. Borges

User avatar
zEal
Dragon
Posts: 944
Joined: Sun Mar 22, 2009 2:25 am

Re: Regular expressions: Simple examples?

Post by zEal »

Which version of MapTool are you using and how are you getting this list of tokens that you want to 'clean'?

User avatar
Daniel
Giant
Posts: 132
Joined: Thu Nov 20, 2008 3:49 am
Location: Germany (GMT +1)

Re: Regular expressions: Simple examples?

Post by Daniel »

I just found the "startsWith(str, substr)" function, which should do in this specific case.

I'm using b53 and using "getVisibleTokenNames()" to get a list of all visible token names.

Assuming "startsWith" was not available (I guess it was made specifically for things like lib:tokens), how would I encode the "starts with" logic in a reg ex? I looked through some guides I found via Google, but all of them got really technical immediately and did not give sufficiently dumbed-down examples.
"This web of time [...] embraces every possibility. We do not exist in most of them. In some you exist and not I, while in others I do, and you do not. [...] In yet another, I say these very same words, but am an error, a phantom."

J.L. Borges

User avatar
zEal
Dragon
Posts: 944
Joined: Sun Mar 22, 2009 2:25 am

Re: Regular expressions: Simple examples?

Post by zEal »

You'd start it with ^ which tells it that the expression must occur at the beginning, then you'd need to escape the colon with a \

You'd end up with "^lib\:" or "^image\:"

I always just make a special map called System to keep all my lib:tokens and image:tokens /shrug.

User avatar
Brigand
Read-only User
Posts: 1623
Joined: Thu Feb 14, 2008 8:57 am
Location: Nosy GM's can go frak themselves!

Re: Regular expressions: Simple examples?

Post by Brigand »

Code: Select all

[h: List = macro.args]
[h: i = 0]
[h, while(listCount(List) > i), CODE:
{
  [h: Current = listGet(List, i)]
  [h, token(Current): isPC = isPC()]
  [h, token(Current): Status = STUFF*]
  [if(isPC == 0 && Status == 0), CODE:
    { [h: i = i + 1] };
    { [h: List = listDelete(List, i)] };
  ]
}]
[h: macro.return = List]
That's what I use to filter the list of targets. Status can be whatever you need it to be. States, names, etc. The name list is generated by: [h: NameList = getVisibleTokenNames()] and passed through a macro roll option.

User avatar
Daniel
Giant
Posts: 132
Joined: Thu Nov 20, 2008 3:49 am
Location: Germany (GMT +1)

Re: Regular expressions: Simple examples?

Post by Daniel »

Cheers.

Wasn't sure if placing the icons on a different map worked, I mainly just wanted a really simple aplication of regular expressions. Works fine now.
"This web of time [...] embraces every possibility. We do not exist in most of them. In some you exist and not I, while in others I do, and you do not. [...] In yet another, I say these very same words, but am an error, a phantom."

J.L. Borges

User avatar
Rumble
Deity
Posts: 6235
Joined: Tue Jul 01, 2008 7:48 pm

Re: Regular expressions: Simple examples?

Post by Rumble »

Brigand wrote: That's what I use to filter the list of targets. Status can be whatever you need it to be. States, names, etc. The name list is generated by: [h: NameList = getVisibleTokenNames()] and passed through a macro roll option.

An alternate filtering method is to use getTokenNames() and the "JSON Filtering" options. They're listed on the wiki here but not explained (the article is a stub). However, they're explained in greater detail in this post; they're the first thing discussed in "Changes."

I use them with great success - in my particular use, the filtering enables me to get (in plain English) "all NPC tokens that are visible and between 1 and maxRange squares from the player token." Since I've used them, I'll attempt to write some examples later today for the wiki.


Note that Brigand's method works perfectly well, too, and is more flexible in one respect because you can set status to be anything, while the JSON method has a set of possible statuses/characteristics from which to choose. The biggest advantage I find in the JSON method is that the list is "prefiltered" with one command.

User avatar
Brigand
Read-only User
Posts: 1623
Joined: Thu Feb 14, 2008 8:57 am
Location: Nosy GM's can go frak themselves!

Re: Regular expressions: Simple examples?

Post by Brigand »

I am an agile scripter! Rawr.

Post Reply

Return to “Documentation Requests/Discussion”