playMaker

Author Topic: RegEx String Comparisons  (Read 4231 times)

Jos Yule

  • Playmaker Newbie
  • *
  • Posts: 33
RegEx String Comparisons
« on: May 14, 2012, 01:39:29 PM »
Hello all.

I'm having to do some string comparisons, and i'm finding the current string compare action too limiting.

I would like to use some kind of RegEx-ish tools in my comparisons. For example, "does the string start with a 'h' character?" or "is the 3rd character an 'a'?".

I don't know if it would just be easier to use the Regex libs, or come up with some other tool/ui/format... Having a RegEx action would be great, but if there is a more friendly way to allow for more complex text comparisons that's cool with me too.

Thanks
Jos

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: RegEx String Comparisons
« Reply #1 on: May 14, 2012, 04:00:46 PM »
Hi,

Using regex would be fine just like a lot of other ways. I can recall some custom actions that I created that use the "split" function as well.

From your examples it looks like you would need the following:

 http://msdn.microsoft.com/en-us/library/3y21t6y4.aspx

Yes? Would that help you if I create a custom action for that regex method?

Because there are so many ways to use Regex, a series of action would be needed as opposed a one that does it all.

 Bye,

 Jean

Jos Yule

  • Playmaker Newbie
  • *
  • Posts: 33
Re: RegEx String Comparisons
« Reply #2 on: May 16, 2012, 09:43:52 AM »
Yes, IsMatch() would be awesome.

My current use-case is simple matching. If i need to access the more complex parts of the RegEx lib in the future, having an example action to build on would be enough. I wouldn't want to try to figure out the other actions without knowing how I, or anyone else, might actually use them. Especially as, you said, there are so many ways to RegEx!

Thanks!
Jos




jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: RegEx String Comparisons
« Reply #3 on: May 17, 2012, 02:21:09 AM »
Hi,

 Here we go:

As you said, take this as a template, making other regex calls will only differs a little bit,  some regex will return a string for example.


Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2011. All rights reserved.

using UnityEngine;
using System.Text.RegularExpressions;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory("Regex")]
[Tooltip("Performs a IsMatch on a String Variable.")]
public class RegexIsMatch: FsmStateAction
{
[RequiredField]

public FsmString stringVariable;

[RequiredField]
public FsmString regexExpression;


[UIHint(UIHint.Variable)]
public FsmBool storeResult;

public FsmEvent isMatchEvent;

public FsmEvent isNotMatchEvent;

public override void Reset()
{
stringVariable = null;
regexExpression = null;
storeResult = null;
isMatchEvent = null;
isNotMatchEvent = null;
}

public override void OnEnter()
{
DoIsMatch();

Finish();
}
void DoIsMatch()
{
if (stringVariable == null) return;
if (regexExpression == null) return;


bool result = Regex.IsMatch(stringVariable.Value,@regexExpression.Value);

storeResult.Value = result;

if (result && isMatchEvent !=null)
{
Fsm.Event(isMatchEvent);

}else if ( result == false && isNotMatchEvent != null){
Fsm.Event(isNotMatchEvent);
}


}

}
}



 If you need regex that returns several strings, then I suggest you use ArrayMaker for this, I can give you a template action for this too ( that would be great actually, running a reg ex within an array of string and store the results in another array).

Bye,

 Jean

Jos Yule

  • Playmaker Newbie
  • *
  • Posts: 33
Re: RegEx String Comparisons
« Reply #4 on: May 17, 2012, 10:24:16 AM »
Ah, this is excellent, thanks! If i write an action to use other functions of the RegEx lib i'll write back here too.

Thanks!
Jos

ps. I love that the actions are simple .cs files - makes creating your own much easier! Having such a wide array of examples to work from is hugely helpful. Really appreciate you writing this one up too!