Skip to content

Instantly share code, notes, and snippets.

@iwashihead
Created September 30, 2016 08:51
Show Gist options
  • Save iwashihead/027afe62492308e2f1d7b8b327207663 to your computer and use it in GitHub Desktop.
Save iwashihead/027afe62492308e2f1d7b8b327207663 to your computer and use it in GitHub Desktop.
正規表現のサンプル
using System;
using UnityEngine;
using System.Collections;
using System.Text.RegularExpressions;
namespace Griphone.Agito.Sandbox
{
public class RegexSample : MonoBehaviour
{
// 正規表現のターゲット文字列.
public string[] targets =
{
"b_01_f@001_stand",
"b_01_f@061_idle_fn",
"b_01_f@061_idle_lp",
"b_01_f@061_idle_st"
};
public enum GenderType
{
Female = 0,
Male = 1,
}
public int JobTypeId;
public GenderType Gender;
public int MotionId;
public bool IsLoop;
public string MotionName;
// Use this for initialization
IEnumerator Start()
{
var waitForOneSec = new WaitForSeconds(1f);
yield return waitForOneSec;
TryParse(targets[0]);
yield return waitForOneSec;
TryParse(targets[1]);
yield return waitForOneSec;
TryParse(targets[2]);
yield return waitForOneSec;
TryParse(targets[3]);
}
void TryParse(string regexTarget)
{
try
{
Regex regex = new Regex("b_(?<JobTypeId>\\d{2})_(?<Gender>[fm])@(?<MotionId>\\d{3})(?<MotionName>.*)");
if (regex.IsMatch(regexTarget))
{
var match = regex.Match(regexTarget);
JobTypeId = int.Parse(match.Groups["JobTypeId"].Value);
Gender = match.Groups["Gender"].Value == "f" ? GenderType.Female : GenderType.Male;
MotionId = int.Parse(match.Groups["MotionId"].Value);
MotionName = match.Groups["MotionName"].Value;
IsLoop = MotionName.ToLower().Contains("lp");
Debug.Log(string.Format("JobTypeId : {0} Gender : {1} MotionId : {2} MotionName : {3} IsLoop : {4}", JobTypeId, Gender, MotionId, MotionName, IsLoop));
}
else
{
Debug.Log("Regex dont match. : " + regex);
}
}
catch (Exception e)
{
Debug.LogError(e.StackTrace);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment