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.

System instructions

You are an expert regex string creator and understand how regex works. Your job is to convert the user's natural language queries and constraints in the form of regex. After generating the regex string, provide explanation in detail with a few examples. Then demonstrate its use in a python code.

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

Of course! As a regex expert, I can certainly translate your requirements into a precise regular expression. This is a common and practical use case, similar to validating hostnames or usernames.

Here is the regex that fulfills all your requirements.

The Regex String

^[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$

Detailed Explanation

Let's break down this regex piece by piece to understand how it works.

Component Description
^ Start Anchor: Asserts that the match must start at the very beginning of the string.
[a-zA-Z0-9] First Character: This ensures the string starts with an alphanumeric character (a-z, A-Z, 0-9). This single character accounts for a length of 1.
(...)? Optional Group: This is a capturing group (...) made optional by the ? quantifier. This structure cleverly handles strings of length 2 to 63. If the string has only two characters, this group is not matched. If it has more, this group must match.
[a-zA-Z0-9-]{0,61} Middle Characters: Inside the group, this allows alphanumeric characters and the hyphen. The quantifier {0,61} means it can match between 0 and 61 times. Why 0 to 61? Because the total length must be 2-63. We already have one character at the start and one at the end. So, 63 - 2 = 61 is the maximum number of characters allowed in the middle. A value of 0 allows for a total string length of 2.
[a-zA-Z0-9] Last Character: This is the second part of the optional group. It ensures that if the string is longer than one character, it must end with an alphanumeric character, not a hyphen.
$ End Anchor: Asserts that the match must end at the very end of the string.

Examples

Valid Strings (Will Match)

  • a1 (Length 2, starts and ends with alphanumeric)
  • my-domain (Contains a hyphen, but starts and ends with letters)
  • example-123 (Mix of letters, numbers, and a hyphen)
  • a-b (Length 3, valid structure)
  • a-long-string-with-numbers-123-and-exactly-63-characters-abc (Exactly 63 characters, valid format)
  • justletters (No hyphens, which is perfectly fine)

Invalid Strings (Will Not Match)

  • a (Fails length constraint: too short)
  • -starts-with-hyphen (Fails start character constraint)
  • ends-with-hyphen- (Fails end character constraint)
  • string-with--double-hyphens (This would actually be valid with this regex, as the rule was only about start/end characters, not consecutive hyphens. If you wanted to forbid that, the regex would need to be more complex.)
  • invalid_char (Contains an underscore _, which is not in [a-zA-Z0-9-])
  • a-string-that-is-definitely-going-to-be-way-too-long-for-this-regex (Fails length constraint: too long)
  • (An empty string, fails all constraints)

Python Code Demonstration

Here is how you would use this regex in a Python script with the re module. We use re.fullmatch() because it checks if the entire string matches the pattern, which is exactly what the ^ and $ anchors are for.

import re

def validate_string(input_string):
    """
    Validates a string based on the specified regex rules.
    
    Args:
        input_string: The string to validate.
        
    Returns:
        True if the string is valid, False otherwise.
    """
    # Regex:
    # ^                  - Start of string
    # [a-zA-Z0-9]        - Must start with an alphanumeric character
    # (
    #   [a-zA-Z0-9-]{0,61} - Followed by 0 to 61 alphanumeric chars or hyphens
    #   [a-zA-Z0-9]        - Followed by one alphanumeric character
    # )?                 - This whole middle group is optional (for strings of length 2)
    # $                  - End of string
    #
Model: gemini-2.5-pro
Temperature: 0.2
Max output tokens: 8192
TopK: 40.0
TopP: 0.95