getGroup: Difference between revisions
No edit summary |
m (Tweaking page layout and typography) |
||
Line 2: | Line 2: | ||
|name=getGroup | |name=getGroup | ||
|version=1.3b48 | |version=1.3b48 | ||
|description=Returns the specified capture group for the specified match that was found using [[strfind|strfind()]] | |description=Returns the specified capture group for the specified match that was found using [[strfind|strfind()]]. | ||
|usage= | |usage= | ||
Line 9: | Line 9: | ||
</source> | </source> | ||
Where | Where | ||
{{param|id|is the id returned by [[strfind|strfind()]]}} | |||
{{param|match|is the number of the match found by [[strfind|strfind()]]}} | |||
{{param|group|is the number of the capture group found by [[strfind|strfind()]]}} | |||
|example= | |example= | ||
Line 23: | Line 23: | ||
match 2, group 2 = [getGroup(id, 2, 2)]<br> | match 2, group 2 = [getGroup(id, 2, 2)]<br> | ||
</source> | </source> | ||
Returns | Returns: | ||
<pre> | <pre> | ||
match 1, group 0 = this is | match 1, group 0 = this is | ||
Line 35: | Line 35: | ||
'''Example explained''' | '''Example explained''' | ||
First off, | First off, normally you only need one {{code|\}} in a regex statement, but because MT uses regex itself and the statement is preparsed you need to double escape it, so {{code|\\}}. | ||
*S = 'everything that is NOT a whitespace' | *{{code|S}} = 'everything that is NOT a whitespace' | ||
*s = 'whitespace | *{{code|s}} = 'whitespace | ||
*+ = '1 or more' | *{{code|+}} = '1 or more' | ||
* | *{{code|*}} = '0 or more' | ||
Have a look [http://www.addedbytes.com/download/regular-expressions-cheat-sheet-v2/png/ here] for an overview | Have a look [http://www.addedbytes.com/download/regular-expressions-cheat-sheet-v2/png/ here] for an overview. | ||
So \\S means grab the first none-whitespace you encounter, | So {{code|\\S}} means grab the first none-whitespace you encounter, {{code|\\S+}} means grap the first none-whitespace you encounter AND ALL characters after that until you encounter a whitespace. | ||
\\S+ means grap the first none-whitespace you encounter AND ALL characters after that until you encounter a whitespace | So the regex statement looks for ''(word)(whitespace)(word)(0 or more whitespace)''. This will deliver 2 matches: {{code|"this is"}} and {{code|"a test"}}. The first match is match 1, the second match 2. | ||
So the regex statement looks for (word)(whitespace)(word)(0 or more whitespace). This will deliver 2 matches: | |||
"this is" and "a test". The first match is match 1, the second match 2. | |||
Within one match there are 1 or more groups: | Within one match there are 1 or more groups: | ||
* The first group '0' returns the ENTIRE match. | * The first group {{code|'0'}} returns the ENTIRE match. | ||
* Every group after that will return partial matches that are within (). | * Every group after that will return partial matches that are within {{code|()}}. | ||
So group '1' will return the first (\\S) part and group '2' will return the second (\\S) of the regex statement. These are respectively (for the first match): "this" and "is" | So group {{code|'1'}} will return the first {{code|(\\S)}} part and group {{code|'2'}} will return the second {{code|(\\S)}} of the regex statement. These are respectively (for the first match): {{code|"this"}} and {{code|"is"}}. | ||
[http://www.gskinner.com/RegExr/ Here a link] to test your regex statements (remember that for this applet you only use one \ while in MT you need \\ | [http://www.gskinner.com/RegExr/ Here a link] to test your regex statements (remember that for this applet you only use one {{code|\}} while in MT you need {{code|\\}}. | ||
}} | }} | ||
[[Category:String Function]] | [[Category:String Function]] |
Revision as of 20:14, 8 October 2012
getGroup() Function
Usage
getGroup(id, match, group)
Where
id
- is the id returned by strfind()match
- is the number of the match found by strfind()group
- is the number of the capture group found by strfind()
Example
[h: id = strfind("this is a test", "(\\S+)\\s(\\S+)\\s*")]
match 1, group 0 = [getGroup(id, 1, 0)]<br>
match 1, group 1 = [getGroup(id, 1, 1)]<br>
match 1, group 2 = [getGroup(id, 1, 2)]<br>
match 2, group 0 = [getGroup(id, 2, 0)]<br>
match 2, group 1 = [getGroup(id, 2, 1)]<br>
match 2, group 2 = [getGroup(id, 2, 2)]<br>
Returns:
match 1, group 0 = this is match 1, group 1 = this match 1, group 2 = is match 2, group 0 = a test match 2, group 1 = a match 2, group 2 = test
Example explained
First off, normally you only need one \
in a regex statement, but because MT uses regex itself and the statement is preparsed you need to double escape it, so \\
.
S
= 'everything that is NOT a whitespace's
= 'whitespace+
= '1 or more'*
= '0 or more'
Have a look here for an overview.
So \\S
means grab the first none-whitespace you encounter, \\S+
means grap the first none-whitespace you encounter AND ALL characters after that until you encounter a whitespace.
So the regex statement looks for (word)(whitespace)(word)(0 or more whitespace). This will deliver 2 matches: "this is"
and "a test"
. The first match is match 1, the second match 2.
Within one match there are 1 or more groups:
- The first group
'0'
returns the ENTIRE match. - Every group after that will return partial matches that are within
()
.
So group '1'
will return the first (\\S)
part and group '2'
will return the second (\\S)
of the regex statement. These are respectively (for the first match): "this"
and "is"
.
\
while in MT you need \\
.