はじめに
正規表現の練習ができるツールを探してもなかなか良いものが見つかりません。
無いなら作ってしまえということで、Unity上で正規表現の練習ができるウィンドウを作ってみました。
UnityのバージョンはUnity5.5.0b7です。
つくったものについて
Textにチェックしたい文字列、Patternに正規表現を入力してcheckボタンを押すと正規表現にマッチする文字列がConsoleに出力されます
コード
以下のスクリプトをプロジェクトの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);
}
}
}
}
}