playMaker

Author Topic: String Regex Replace  (Read 3989 times)

Krileon

  • Full Member
  • ***
  • Posts: 107
String Regex Replace
« on: April 13, 2014, 02:57:31 PM »
I wanted to use REGEX for my replacement usage so I created a String Regex Replace. Thought I'd go ahead and share it as it's a pretty generic and useful action.

[edit] It's now available directly on the Ecosystem

StringRegexReplace

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

namespace HutongGames.PlayMaker.Actions {
[ActionCategory(ActionCategory.String)]
public class StringRegexReplace : FsmStateAction {
[RequiredField, UIHint(UIHint.Variable)]
public FsmString stringVariable;
public FsmString regex;
public FsmString with;
public RegexOptions[] options;
[RequiredField, UIHint(UIHint.Variable)]
public FsmString storeResult;
public bool everyFrame;

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

public override void OnEnter() {
DoAction();

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

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

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

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

storeResult.Value = Regex.Replace( stringVariable.Value, regex.Value, with.Value, optionsBit );
} else {
storeResult.Value = Regex.Replace( stringVariable.Value, regex.Value, with.Value );
}
}
}
}

I'll probably be adding other regex actions as well such as Match. Anyway, hope others find as much use out of this as I have.
« Last Edit: September 08, 2015, 08:35:47 AM by jeanfabre »

Krileon

  • Full Member
  • ***
  • Posts: 107
Re: String Regex Replace
« Reply #1 on: April 13, 2014, 03:15:13 PM »
Ok, created a String Regex Is Match action, which you can find below.

http://hutonggames.com/playmakerforum/index.php?topic=7005.0

Krileon

  • Full Member
  • ***
  • Posts: 107
Re: String Regex Replace
« Reply #2 on: April 13, 2014, 03:25:57 PM »
New String Regex Match action can be found below.

http://hutonggames.com/playmakerforum/index.php?topic=7006.0

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: String Regex Replace
« Reply #3 on: September 08, 2015, 08:36:19 AM »
Hi,

 Thanks for this. I made it available on the Ecosystem for convenience.

 Bye,

 Jean