@ -35,10 +35,10 @@ func (m Match) numValidGroups() int {
}
}
// Returns a string containing the indices of all (valid) groups in the match
// Returns a string containing the indices of all (valid) groups in the match
func ( m Match ) To String( ) string {
func ( m Match ) String( ) string {
var toRet string
var toRet string
for i , g := range m {
for i , g := range m {
if g . i sValid( ) {
if g . I sValid( ) {
toRet += fmt . Sprintf ( "Group %d\n" , i )
toRet += fmt . Sprintf ( "Group %d\n" , i )
toRet += g . toString ( )
toRet += g . toString ( )
toRet += "\n"
toRet += "\n"
@ -52,8 +52,9 @@ func (idx Group) toString() string {
return fmt . Sprintf ( "%d\t%d" , idx . StartIdx , idx . EndIdx )
return fmt . Sprintf ( "%d\t%d" , idx . StartIdx , idx . EndIdx )
}
}
// Returns whether a group contains valid indices
// Returns whether a group is valid (ie. whether it matched any text). It
func ( g Group ) isValid ( ) bool {
// simply ensures that both indices of the group are >= 0.
func ( g Group ) IsValid ( ) bool {
return g . StartIdx >= 0 && g . EndIdx >= 0
return g . StartIdx >= 0 && g . EndIdx >= 0
}
}
@ -174,6 +175,20 @@ func (regex Reg) FindString(str string) string {
return str [ zeroGroup . StartIdx : zeroGroup . EndIdx ]
return str [ zeroGroup . StartIdx : zeroGroup . EndIdx ]
}
}
// FindSubmatch returns the leftmost match of the regex in the given string, including
// the submatches matched by capturing groups. The returned [Match] will always contain the same
// number of groups. The validity of a group (whether or not it matched anything) can be determined with
// [Group.IsValid], or by checking that both indices of the group are >= 0.
// The second-return value is nil if no match was found.
func ( regex Reg ) FindSubmatch ( str string ) ( Match , error ) {
match , err := regex . FindNthMatch ( str , 1 )
if err != nil {
return Match { } , fmt . Errorf ( "no match found" )
} else {
return match , nil
}
}
// FindAllString is the 'all' version of FindString.
// FindAllString is the 'all' version of FindString.
// It returns a slice of strings containing the text of all matches of
// It returns a slice of strings containing the text of all matches of
// the regex in the given string.
// the regex in the given string.
@ -372,7 +387,7 @@ func findAllSubmatchHelper(start *nfaState, str []rune, offset int, numGroups in
// Check if we can find a zero-length match
// Check if we can find a zero-length match
if foundPath == false {
if foundPath == false {
if ok := zeroMatchPossible ( str , i , numGroups , currentStates ... ) ; ok {
if ok := zeroMatchPossible ( str , i , numGroups , currentStates ... ) ; ok {
if tempIndices [ 0 ] . i sValid( ) == false {
if tempIndices [ 0 ] . I sValid( ) == false {
tempIndices [ 0 ] = Group { startIdx , startIdx }
tempIndices [ 0 ] = Group { startIdx , startIdx }
}
}
}
}
@ -382,7 +397,7 @@ func findAllSubmatchHelper(start *nfaState, str []rune, offset int, numGroups in
startIdx ++
startIdx ++
// i++
// i++
// }
// }
if tempIndices . numValidGroups ( ) > 0 && tempIndices [ 0 ] . i sValid( ) {
if tempIndices . numValidGroups ( ) > 0 && tempIndices [ 0 ] . I sValid( ) {
if tempIndices [ 0 ] . StartIdx == tempIndices [ 0 ] . EndIdx { // If we have a zero-length match, we have to shift the index at which we start. Otherwise we keep looking at the same paert of the string over and over.
if tempIndices [ 0 ] . StartIdx == tempIndices [ 0 ] . EndIdx { // If we have a zero-length match, we have to shift the index at which we start. Otherwise we keep looking at the same paert of the string over and over.
return true , tempIndices , tempIndices [ 0 ] . EndIdx + 1
return true , tempIndices , tempIndices [ 0 ] . EndIdx + 1
} else {
} else {