8000 feat: add support for regexp in xpath replace function by Mrflatt · Pull Request #107 · antchfx/xpath · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: add support for regexp in xpath replace function #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion func.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,8 +564,17 @@ func replaceFunc(arg1, arg2, arg3 query) func(query, iterator) interface{} {
str := asString(t, functionArgs(arg1).Evaluate(t))
src := asString(t, functionArgs(arg2).Evaluate(t))
dst := asString(t, functionArgs(arg3).Evaluate(t))
e, err := getRegexp(src)
if err != nil {
panic(fmt.Errorf("replace() function second argument is not a valid regexp pattern, err: %s", err.Error()))
}

// replace all $i to ${i} for golang regexp.Expand
for idx := e.NumSubexp(); idx > 0; idx-- {
dst = strings.ReplaceAll(dst, fmt.Sprintf("$%d", idx), fmt.Sprintf("${%d}", idx))
}

return strings.Replace(str, src, dst, -1)
return e.ReplaceAllString(str, dst)
}
}

Expand Down
17 changes: 10 additions & 7 deletions xpath_function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,16 @@ func Test_func_replace(t *testing.T) {
test_xpath_eval(t, empty_example, `replace("abracadabra", "a", "")`, "brcdbr")
// The below xpath expressions is not supported yet
//
//test_xpath_eval(t, empty_example, `replace("abracadabra", "a.*a", "*")`, "*")
//test_xpath_eval(t, empty_example, `replace("abracadabra", "a.*?a", "*")`, "*c*bra")
//test_xpath_eval(t, empty_example, `replace("abracadabra", ".*?", "$1")`, "*c*bra") // error, because the pattern matches the zero-length string
//test_xpath_eval(t, empty_example, `replace("AAAA", "A+", "b")`, "b")
//test_xpath_eval(t, empty_example, `replace("AAAA", "A+?", "b")`, "bbb")
//test_xpath_eval(t, empty_example, `replace("darted", "^(.*?)d(.*)$", "$1c$2")`, "carted")
//test_xpath_eval(t, empty_example, `replace("abracadabra", "a(.)", "a$1$1")`, "abbraccaddabbra")
test_xpath_eval(t, empty_example, `replace("abracadabra", "a.*a", "*")`, "*")
test_xpath_eval(t, empty_example, `replace("abracadabra", "a.*?a", "*")`, "*c*bra")
// test_xpath_eval(t, empty_example, `replace("abracadabra", ".*?", "$1")`, "*c*bra") // error, because the pattern matches the zero-length string
test_xpath_eval(t, empty_example, `replace("AAAA", "A+", "b")`, "b")
test_xpath_eval(t, empty_example, `replace("AAAA", "A+?", "b")`, "bbbb")
test_xpath_eval(t, empty_example, `replace("darted", "^(.*?)d(.*)$", "$1c$2")`, "carted")
test_xpath_eval(t, empty_example, `replace("abracadabra", "a(.)", "a$1$1")`, "abbraccaddabbra")
test_xpath_eval(t, empty_example, `replace("abcd", "(ab)|(a)", "[1=$1][2=$2]")`, "[1=ab][2=]cd")
test_xpath_eval(t, empty_example, `replace("1/1/c11/1", "(.*)/[^/]+$", "$1")`, "1/1/c11")
test_xpath_eval(t, empty_example, `replace("A/B/C/D/E/F/G/H/I/J/K/L", "([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/(.*)", "$1-$2-$3-$4-$5-$6-$7-$8-$9-$10")`, "A-B-C-D-E-F-G-H-I-J/K/L")
}

func Test_func_reverse(t *testing.T) {
Expand Down
0