playMaker

Author Topic: String Regex Match  (Read 2734 times)

Krileon

  • Full Member
  • ***
  • Posts: 107
String Regex Match
« on: April 13, 2014, 03:25:36 PM »
Yet another string regex action. This one, similar to String Regex Is Match, can do a true/false event based off the match, but it also stores the result of the match to another string variable instead of storing true/false to boolean.

[edit] You can now get it directly from the Ecosystem

StringRegexMatch

Code: [Select]
using UnityEngine;
using System.Text.RegularExpressions;

namespace HutongGames.PlayMaker.Actions {
[ActionCategory(ActionCategory.String)]
public class StringRegexMatch : FsmStateAction {
[RequiredField, UIHint(UIHint.Variable)]
public FsmString stringVariable;
public FsmString regex;
public RegexOptions[] options;
[UIHint(UIHint.Variable)]
public FsmString storeMatch;
[UIHint(UIHint.Variable)]
public FsmEvent trueEvent;
[UIHint(UIHint.Variable)]
public FsmEvent falseEvent;
public bool everyFrame;

public override void Reset() {
stringVariable = null;
regex = new FsmString { Value = "" };
options = new RegexOptions[0];
storeMatch = null;
trueEvent = null;
falseEvent = null;
everyFrame = false;
}

public override void OnEnter() {
DoAction();

if ( ! everyFrame ) {
Finish();
}
}

public override void OnUpdate() {
DoAction();
}

void DoAction() {
Match match = null;

if ( options.Length > 0 ) {
RegexOptions optionsBit = 0;

foreach ( RegexOptions option in options ) {
optionsBit |= option;
}

match = Regex.Match( stringVariable.Value, regex.Value, optionsBit );
} else {
match = Regex.Match( stringVariable.Value, regex.Value );
}

storeMatch.Value = match.Value;

if ( match.Success ) {
if ( trueEvent != null ) {
Fsm.Event( trueEvent );
}
} else {
if ( falseEvent != null ) {
Fsm.Event( falseEvent );
}
}
}
}
}

You can find my other String Regex actions as follows.

String Regex Is Match:
http://hutonggames.com/playmakerforum/index.php?topic=7005.0

String Regex Replace:
http://hutonggames.com/playmakerforum/index.php?topic=7004.0
« Last Edit: September 08, 2015, 08:24:29 AM by jeanfabre »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: String Regex Match
« Reply #1 on: September 08, 2015, 08:24:56 AM »
Hi,

 Thanks for this. I have put it on the Ecosystem for convenience.

 Bye,

 Jean