LoginSignup
8
3

More than 5 years have passed since last update.

【Unity拡張】正規表現の練習ができるツールを作ってみた

Posted at

はじめに

正規表現の練習ができるツールを探してもなかなか良いものが見つかりません。
無いなら作ってしまえということで、Unity上で正規表現の練習ができるウィンドウを作ってみました。

UnityのバージョンはUnity5.5.0b7です。

つくったものについて

image
Textにチェックしたい文字列、Patternに正規表現を入力してcheckボタンを押すと正規表現にマッチする文字列がConsoleに出力されます

image

コード

以下のスクリプトをプロジェクトのEditorフォルダ以下に入れてください

RegexWindow.cs
namespace hoge
{
    using UnityEngine;
    using UnityEditor;
    using System.Collections;
    using System.Text.RegularExpressions;

    public class RegexWindow : EditorWindow 
    {
        private string text;
        private string pattern;

        [MenuItem("EditorWindow/RegexWindow")]
        private static void Open()
        {
            var window = ScriptableObject.CreateInstance<RegexWindow>();
            window.titleContent.text = "RegexWindow";
            window.Show();
        }

        private void OnGUI()
        {
            var option = GUILayout.Width(80f);

            GUILayout.Label("Text", option);
            this.text = EditorGUILayout.TextArea(this.text);

            GUILayout.Space(6f);
            GUILayout.Label("Pattern", option);
            this.pattern = EditorGUILayout.TextArea(this.pattern);

            if (GUILayout.Button("check"))
            {
                // マッチする文字列をConsoleへ出力
                var matches = Regex.Matches(this.text, this.pattern);
                for (int i = 0; i < matches.Count; i++)
                {
                    Debug.Log(matches[i].Value);
                }
            }
        }
    }
}

8
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
3