BaH.RegEx - Named Capture Groups

BlitzMax Forums/Brucey's Modules/BaH.RegEx - Named Capture Groups

Scaremonger(Posted 2014) [#1]
The BaH.RegEx module already supports named capture groups in the format (?<name>..) but the functions to use them have not been exposed in the module.

If you wish to use them; First add the following method to Type TRegEx in regex.bmx:
	'# Obtain match number using capture name
	'# Si Dunford [Scaremonger] - Aug 2014
	Method getStringNumber:Int( captureName:String )
	Local name:Byte Ptr = captureName.toWString()
	Local index:Int = pcre16_get_stringnumber(pcre, name);
		MemFree( name )
		Return index
	End Method


Secondly: Add the following line to the Extern section at the bottom of common.bmx:
	'# Si Dunford [Scaremonger] Aug 2014
	Function pcre16_get_stringnumber:Int( pcre:Byte Ptr, name:Byte Ptr )


Now re-compile the module.

You can extract the capture index number of a named group with a single call as follows:
SuperStrict

Import bah.regex

Local str$ = "yet another test"
Local match:TRegExMatch
Local regex:TRegEx = TRegEx.Create( "(?<word>\w*)" )
local index:int

Try
	match = regex.Find( str )
Catch e:TRegExException
	If e.num <> REGEX_NOMATCH Then
		Print "Error : " + e.toString()
		End
	Else
		Print "No Match"
	End If
End Try

If match Then
	index = regex.getStringNumber("word")
	Print index + ". " + match.SubExp(index)
End If
Print "Done."



Brucey(Posted 2014) [#2]
Thanks, I'll have a look at implementing it.