1 /**
2 Class for searching text for patterns using regular expressions.
3 
4 Copyright:
5 Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur.  
6 Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md)  
7 Copyright (c) 2017-2018 Godot-D contributors  
8 
9 License: $(LINK2 https://opensource.org/licenses/MIT, MIT License)
10 
11 
12 */
13 module godot.regex;
14 import std.meta : AliasSeq, staticIndexOf;
15 import std.traits : Unqual;
16 import godot.d.meta;
17 import godot.core;
18 import godot.c;
19 import godot.d.bind;
20 import godot.d.reference;
21 import godot.object;
22 import godot.classdb;
23 import godot.reference;
24 import godot.regexmatch;
25 /**
26 Class for searching text for patterns using regular expressions.
27 
28 Regular Expression (or regex) is a compact programming language that can be used to recognise strings that follow a specific pattern, such as URLs, email addresses, complete sentences, etc. For instance, a regex of `ab$(D 0-9)` would find any string that is `ab` followed by any number from `0` to `9`. For a more in-depth look, you can easily find various tutorials and detailed explanations on the Internet.
29 To begin, the RegEx object needs to be compiled with the search pattern using $(D compile) before it can be used.
30 
31 
32 var regex = RegEx.new()
33 regex.compile("\\w-(\\d+)")
34 
35 
36 The search pattern must be escaped first for gdscript before it is escaped for the expression. For example, `compile("\\d+")` would be read by RegEx as `\d+`. Similarly, `compile("\"(?:\\\\.|$(D ^\"))*\"")` would be read as `"(?:\\.|$(D ^"))*"`
37 Using $(D search) you can find the pattern within the given text. If a pattern is found, $(D RegExMatch) is returned and you can retrieve details of the results using functions such as $(D RegExMatch.getString) and $(D RegExMatch.getStart).
38 
39 
40 var regex = RegEx.new()
41 regex.compile("\\w-(\\d+)")
42 var result = regex.search("abc n-0123")
43 if result:
44     print(result.get_string()) # Would print n-0123
45 
46 
47 The results of capturing groups `()` can be retrieved by passing the group number to the various functions in $(D RegExMatch). Group 0 is the default and would always refer to the entire pattern. In the above example, calling `result.get_string(1)` would give you `0123`.
48 This version of RegEx also supports named capturing groups, and the names can be used to retrieve the results. If two or more groups have the same name, the name would only refer to the first one with a match.
49 
50 
51 var regex = RegEx.new()
52 regex.compile("d(?<digit>$(D 0-9)+)|x(?<digit>$(D 0-9a-f)+)")
53 var result = regex.search("the number is x2f")
54 if result:
55     print(result.get_string("digit")) # Would print 2f
56 
57 
58 If you need to process multiple results, $(D searchAll) generates a list of all non-overlapping results. This can be combined with a for-loop for convenience.
59 
60 
61 for result in regex.search_all("d01, d03, d0c, x3f and x42"):
62     print(result.get_string("digit"))
63 # Would print 01 03 3f 42
64 # Note that d0c would not match
65 
66 
67 */
68 @GodotBaseClass struct RegEx
69 {
70 	enum string _GODOT_internal_name = "RegEx";
71 public:
72 @nogc nothrow:
73 	union { godot_object _godot_object; Reference _GODOT_base; }
74 	alias _GODOT_base this;
75 	alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses);
76 	package(godot) __gshared bool _classBindingInitialized = false;
77 	package(godot) static struct _classBinding
78 	{
79 		__gshared:
80 		@GodotName("clear") GodotMethod!(void) clear;
81 		@GodotName("compile") GodotMethod!(GodotError, String) compile;
82 		@GodotName("search") GodotMethod!(RegExMatch, String, long, long) search;
83 		@GodotName("search_all") GodotMethod!(Array, String, long, long) searchAll;
84 		@GodotName("sub") GodotMethod!(String, String, String, bool, long, long) sub;
85 		@GodotName("is_valid") GodotMethod!(bool) isValid;
86 		@GodotName("get_pattern") GodotMethod!(String) getPattern;
87 		@GodotName("get_group_count") GodotMethod!(long) getGroupCount;
88 		@GodotName("get_names") GodotMethod!(Array) getNames;
89 	}
90 	bool opEquals(in RegEx other) const { return _godot_object.ptr is other._godot_object.ptr; }
91 	RegEx opAssign(T : typeof(null))(T n) { _godot_object.ptr = null; }
92 	bool opEquals(typeof(null) n) const { return _godot_object.ptr is null; }
93 	mixin baseCasts;
94 	static RegEx _new()
95 	{
96 		static godot_class_constructor constructor;
97 		if(constructor is null) constructor = _godot_api.godot_get_class_constructor("RegEx");
98 		if(constructor is null) return typeof(this).init;
99 		return cast(RegEx)(constructor());
100 	}
101 	@disable new(size_t s);
102 	/**
103 	This method resets the state of the object, as it was freshly created. Namely, it unassigns the regular expression of this object.
104 	*/
105 	void clear()
106 	{
107 		checkClassBinding!(typeof(this))();
108 		ptrcall!(void)(_classBinding.clear, _godot_object);
109 	}
110 	/**
111 	Compiles and assign the search pattern to use. Returns OK if the compilation is successful. If an error is encountered the details are printed to STDOUT and FAILED is returned.
112 	*/
113 	GodotError compile(StringArg0)(in StringArg0 pattern)
114 	{
115 		checkClassBinding!(typeof(this))();
116 		return ptrcall!(GodotError)(_classBinding.compile, _godot_object, pattern);
117 	}
118 	/**
119 	Searches the text for the compiled pattern. Returns a $(D RegExMatch) container of the first matching result if found, otherwise null. The region to search within can be specified without modifying where the start and end anchor would be.
120 	*/
121 	Ref!RegExMatch search(StringArg0)(in StringArg0 subject, in long offset = 0, in long end = -1) const
122 	{
123 		checkClassBinding!(typeof(this))();
124 		return ptrcall!(RegExMatch)(_classBinding.search, _godot_object, subject, offset, end);
125 	}
126 	/**
127 	Searches the text for the compiled pattern. Returns an array of $(D RegExMatch) containers for each non-overlapping result. If no results were found an empty array is returned instead. The region to search within can be specified without modifying where the start and end anchor would be.
128 	*/
129 	Array searchAll(StringArg0)(in StringArg0 subject, in long offset = 0, in long end = -1) const
130 	{
131 		checkClassBinding!(typeof(this))();
132 		return ptrcall!(Array)(_classBinding.searchAll, _godot_object, subject, offset, end);
133 	}
134 	/**
135 	Searches the text for the compiled pattern and replaces it with the specified string. Escapes and backreferences such as `\1` and `\g<name>` expanded and resolved. By default only the first instance is replaced but it can be changed for all instances (global replacement). The region to search within can be specified without modifying where the start and end anchor would be.
136 	*/
137 	String sub(StringArg0, StringArg1)(in StringArg0 subject, in StringArg1 replacement, in bool all = false, in long offset = 0, in long end = -1) const
138 	{
139 		checkClassBinding!(typeof(this))();
140 		return ptrcall!(String)(_classBinding.sub, _godot_object, subject, replacement, all, offset, end);
141 	}
142 	/**
143 	Returns whether this object has a valid search pattern assigned.
144 	*/
145 	bool isValid() const
146 	{
147 		checkClassBinding!(typeof(this))();
148 		return ptrcall!(bool)(_classBinding.isValid, _godot_object);
149 	}
150 	/**
151 	Returns the original search pattern that was compiled.
152 	*/
153 	String getPattern() const
154 	{
155 		checkClassBinding!(typeof(this))();
156 		return ptrcall!(String)(_classBinding.getPattern, _godot_object);
157 	}
158 	/**
159 	Returns the number of capturing groups in compiled pattern.
160 	*/
161 	long getGroupCount() const
162 	{
163 		checkClassBinding!(typeof(this))();
164 		return ptrcall!(long)(_classBinding.getGroupCount, _godot_object);
165 	}
166 	/**
167 	Returns an array of names of named capturing groups in the compiled pattern. They are ordered by appearance.
168 	*/
169 	Array getNames() const
170 	{
171 		checkClassBinding!(typeof(this))();
172 		return ptrcall!(Array)(_classBinding.getNames, _godot_object);
173 	}
174 }