Skip to content
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

Support pseudo pattern literal #13

Open
zero-plusplus opened this issue Sep 16, 2021 · 0 comments
Open

Support pseudo pattern literal #13

zero-plusplus opened this issue Sep 16, 2021 · 0 comments
Labels
draft Draft of new features

Comments

@zero-plusplus
Copy link
Owner

Usually, InStr is used to search for text, and RegExMatch is used to search for regular expressions.

Pattern Literal provides a way for the user to control these types of text matching. This allows we to combine the above functions into one.

The following is a function to search for strings using pattern literal.

TextSearch(str, pattern) {
  pattern := bee.PatternLiteral(pattern)
  if (pattern.type == "text") {
    return InStr(str, pattern.text)
  }
  return RegExMatch(str, pattern.text)
}

Pattern Literal is a simple object that has two types: text and type. This object can also be represented by an array of two lengths, followed by text and type.

The following three types are available.

  • "text" - Plain text
  • "ignorecase" - Plain text, not case-sensitive
  • "regex" - regular expression
    Other than that, I plan to implement "wildcard" if there is demand.

The following is an example of use.

TextSearch("example text", { text: "E", type: "text" }) => 0
TextSearch("example text", [ "E", "text" ]) => 0

TextSearch("example text", { text: "E", type: "ignorecase" }) => 1
TextSearch("example text", [ "E", "ignorecase" ]) => 1

TextSearch("example text", { text: "/E(?=\s)/i", type: "regex" }) => 7
TextSearch("example text", [ "/E(?=\s)/i", "regex" ]) => 7

There is a special format for "regex", which can be specified as "/REGEX/FLAGS".

TextSearch("example text", "/E(?=\s)/i") => 7
@zero-plusplus zero-plusplus added the draft Draft of new features label Sep 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
draft Draft of new features
Projects
None yet
Development

No branches or pull requests

1 participant