Regexed

Convert natural language queries and constraints to regex constructs

You can query a model directly and test the results returned when using different parameter values with the Cloud console, or by calling the Vertex AI API directly.

Prompt Freeform

Give me the regex equivalent of the following:

My requirements are:

  1. I want my string to have 2-63 characters.
  2. The string should be alphanumeric and can contain - also.
  3. The string must start as well as end with alphanumeric characters only.

Response

^[a-zA-Z0-9]{1}[a-zA-Z0-9-]{0,61}[a-zA-Z0-9]{1}$

Explanation:

  • ^: Matches the beginning of the string.
  • [a-zA-Z0-9]{1}: Matches one alphanumeric character (a-z, A-Z, 0-9) at the beginning of the string.
  • [a-zA-Z0-9\-]{0,61}: Matches zero to 61 alphanumeric characters or hyphens (-) in the middle of the string. This ensures the string has a total length of 2-63 characters.
  • [a-zA-Z0-9]{1}: Matches one alphanumeric character at the end of the string.
  • $: Matches the end of the string.

Examples:

  • Valid strings:
  • abc
  • 123-abc
  • abc-123
  • a-b-c-123
  • Invalid strings:
  • -abc (Starts with a hyphen)
  • abc- (Ends with a hyphen)
  • abc123- (Ends with a hyphen)
  • 1234567890123456789012345678901234567890 (More than 63 characters)

Python Code:

import re

regex = r"^[a-zA-Z0-9]{1}[a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]{1}$"

test_strings = [
"abc",
"123-abc",
"abc-123",
"a-b-c-123",
"-abc",
"abc-",
"abc123-",
"1234567890123456789012345678901234567890",
]

for string in test_strings:
match = re.match(regex, string)
if match:
print(f"'{string}' is a valid string.")
else:
print(f"'{string}' is an invalid string.")

This code will print whether each test string is valid or invalid based on the regex pattern.

Model: gemini-1.5-flash-001
Temperature: 1
Max output tokens: 8192
TopK: 40
TopP: 0.95