Skip to main content

regex

Type created by the regex function.

def regex(
regex: str,
/,
*,
fancy: bool = False,
) -> BuckRegex

Compile a regular expression from a string.

Fanciness

Buck2 regexes support two backing implementations:

  • With fancy = False (the default), the regex crate is used. These regular expressions compile down to a DFA and match very quickly and efficiently (in linear time), but do not support look-around or backreferences.

  • With fancy = True, the fancy_regex crate is used, which does support look-around and backreferences, but is slower to match (exponential time in the worst case).


regex.match

def regex.match(
str: str,
/,
) -> bool

Determine if the regex matches any substring of the given string.


regex.replace_all

def regex.replace_all(
haystack: str,
replacement: str,
/,
) -> str

Replace all matches of the regex in the given string with the replacement string. Takes the following parameters: * haystack - The string you will be regex matching against * replacement - The replacement string to replace the regex matches with

Returns a new string with all regex matches replaced.

Sample usage:

regex_pattern = regex(r"foo")
result = regex_pattern.replace_all("foo bar foo", "baz")

result equals "baz bar baz" in this example