Skip to content

[regex] Unable to replace just at the beginning/at the end of a string. #26523

@axbender

Description

@axbender

Describe the bug

Matching at the start/end of a string doesn't work as expected; it seems that ^ and $ are not correctly honored (see
sample below under "Reproduction Steps").

The following Python code does what I intend to do:

import re
g_name = r"_#abc.#_ab#"

g_re = r"(^[#.]+)|([#.]+$)" # Replace either at the beginning or at the end of the string
print(re.sub(g_re, "*", g_name))

Correctly prints: abc.#_ab

g_re = r"^[_#.]+" # Replace only at the beginning of the string
print(re.sub(g_re, "*", g_name))

Correctly prints: *abc.#_ab#

Reproduction Steps

import regex

g_name := r"_#abc.#_ab#"

mut g_re := regex.regex_opt(r"(^[#.]+)|([#.]+$)")!
println(g_re.replace(g_name, "*"))
// Prints: _#abc.#_ab#, i.e. nothing is replaced.

g_re = regex.regex_opt(r"^[_#.]+")!
println(g_re.replace(g_name, ""))
// Prints: abcab
, i.e. EVERY occurrence is replaced. What about the start-of-line anchor?

Expected Behavior

Replace correctly (cmp. the Python results).

Current Behavior

Are documented in "Reproduction Steps".

Possible Solution

Additional Information/Context

No response

V version

0.5.0 6be2fb

Environment details (OS name and version, etc.)

Windows 10.

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

Metadata

Metadata

Assignees

Labels

BugThis tag is applied to issues which reports bugs.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions