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.all_match

def regex.all_match(
strings: list[str],
/,
) -> bool

Determine if the regex matches a substring of every one of the given strings. Returns True for an empty list. Returns as soon as a non-matching element is found, without validating the remaining elements.

Behaves like calling match on each element, but is significantly faster on large lists because it crosses from Starlark into native code only once.


regex.any_match

def regex.any_match(
strings: list[str],
/,
) -> bool

Determine if the regex matches a substring of any of the given strings. Returns False for an empty list. Returns as soon as a match is found, without validating the remaining elements.

Behaves like calling match on each element, but is significantly faster on large lists because it crosses from Starlark into native code only once.


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