Skip to content

Instantly share code, notes, and snippets.

@carloswm85
Created September 3, 2024 01:59
Show Gist options
  • Save carloswm85/e5ce57c0ae3fac8492522b23c0adba69 to your computer and use it in GitHub Desktop.
Save carloswm85/e5ce57c0ae3fac8492522b23c0adba69 to your computer and use it in GitHub Desktop.

This endpoint works:

https://www.binance.com/api/v3/klines?symbol=BTCUSDT&interval=1h&limit=2

This endpoint will return an error:

https://www.binance.com/api/v3/klines?symbol=BTCUSDT%7CETHUSDT&interval=1h&limit=2

image

The regular expression ^[A-Z0-9-_.]{1,20}$ is a pattern used to validate strings, ensuring they adhere to specific rules. Let's break down each part of this regular expression:

Regular Expression Breakdown:

  1. ^ (Caret)

    • Meaning: Asserts the start of the string. It ensures that the match must occur at the beginning of the string.
  2. [A-Z0-9-_.] (Character Class)

    • Meaning: Defines a set of allowed characters.
    • Details:
      • A-Z: Allows any uppercase English letter (from A to Z).
      • 0-9: Allows any digit (from 0 to 9).
      • -: Allows the hyphen character -.
      • _: Allows the underscore character _.
      • .: Allows the period (dot) character ..
  3. {1,20} (Quantifier)

    • Meaning: Specifies that the preceding character class must occur at least 1 time and at most 20 times.
    • Details:
      • 1: The minimum number of characters allowed is 1.
      • 20: The maximum number of characters allowed is 20.
  4. $ (Dollar Sign)

    • Meaning: Asserts the end of the string. It ensures that the match must occur at the end of the string.

Summary:

  • Overall Pattern: The string must consist of 1 to 20 characters, where each character is either an uppercase letter (A-Z), a digit (0-9), a hyphen (-), an underscore (_), or a period (.).
  • String Boundaries: The ^ and $ ensure that the entire string must match this pattern from start to finish, with no additional characters.

Examples of Valid Strings:

  • "BTCUSDT"
  • "ABC-123"
  • "TOKEN_NAME"
  • "X-123.A"

Examples of Invalid Strings:

  • "btc_usdt" (contains lowercase letters, which are not allowed)
  • "ABC!123" (contains an exclamation mark, which is not allowed)
  • "A" (valid but would be an edge case at the minimum length)
  • "TOO-LONG-VALUE-EXAMPLE" (would be invalid if it exceeds 20 characters)

This regular expression is commonly used to validate inputs where a specific format is required, such as in API parameters, filenames, identifiers, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment