mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-19 22:28:45 +05:00
Adding search and replace regexp utils
This commit is contained in:
@@ -225,3 +225,26 @@ func ParseZeroRatingSubject(rateSubj string) (time.Duration, error) {
|
||||
durStr := rateSubj[len(ZERO_RATING_SUBJECT_PREFIX):]
|
||||
return time.ParseDuration(durStr)
|
||||
}
|
||||
|
||||
// Used to parse extra fields definition into regexp rules
|
||||
func ParseSearchReplaceRegexp(strRule string) (*regexp.Regexp, string, error) {
|
||||
// String rule expected in the form ~hdr_name:s/match_rule/replace_rule/
|
||||
getRuleRgxp := regexp.MustCompile(`:s\/(.+[^\\])\/(.+[^\\])\/`) // Make sure the separator / is not escaped in the rule
|
||||
allMatches := getRuleRgxp.FindStringSubmatch(strRule)
|
||||
if len(allMatches) != 3 { // Second and third groups are of interest to us
|
||||
return nil, "", errors.New("Invalid Search&Replace rule.")
|
||||
}
|
||||
searchRegexp, err := regexp.Compile(allMatches[1])
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
return searchRegexp, allMatches[2], nil
|
||||
}
|
||||
|
||||
// Used to expand string sources, eg: extra fields
|
||||
func RegexpSearchReplace(src string, searchRl *regexp.Regexp, replaceTpl string) string {
|
||||
res := []byte{}
|
||||
match := searchRl.FindStringSubmatchIndex(src)
|
||||
res = searchRl.ExpandString(res, replaceTpl, src, match)
|
||||
return string(res)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user