Matching ^
and $
with Regex::captures_at
#1125
-
Hi there! I'm currently building a tiny TextMate-like syntax highlighter using regexes at its core. One thing I need to do is to match the beginning of a substring. For instance: let regex = Regex::new("^@direct");
let input = "call(@direct cmd)";
regex.captures_at(input, 5); // Must match Unfortunately, this doesn't work due to What workaround could be used for this use case? I need something that will allow the regex to match in the exact same conditions (full input, provided position). EDIT: To be clearer, I need a way to specify in the regex that I want to match the beginning or end of the portion of string it is matched against, not the full string. Thanks in advance for your help! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I think the direct and most straight-forward answer to your specific question is Otherwise, if you want to start a search at an arbitrary position while taking look-behind assertions into account but want to limit your matches to those that start at the same position that you started your search, then you'll have to drop down to |
Beta Was this translation helpful? Give feedback.
I think the direct and most straight-forward answer to your specific question is
regex.captures(&input[5..])
.Otherwise, if you want to start a search at an arbitrary position while taking look-behind assertions into account but want to limit your matches to those that start at the same position that you started your search, then you'll have to drop down to
regex-automata
and use its anchored option. The example there uses thePikeVM
, but it will also work with the meta regex engine which is probably what you want to use. (Theregex
crate is itself just a wrapper around the meta regex engine.)