You are viewing a single comment's thread from:

RE: regex

in LeoFinance9 months ago

Let's test you skills, what does this do?

mystery_regex = (
    r"^(?!string$)(?=.{3,16}$)[a-z]([0-9a-z]|[0-9a-z\-](?=[0-9a-z]))"
    r"{2,}([\.](?=[a-z][0-9a-z\-][0-9a-z\-])[a-z]([0-9a-z]"
    r"|[0-9a-z\-](?=[0-9a-z])){1,}){0,}$"
)
Sort:  

"This regular expression (mystery_regex) appears to be a pattern designed to match valid domain names, following the rules and constraints set for domain names. Let's break it down step by step:

  1. Length of the string: (?=.{3,16}$): This is a positive lookahead assertion that ensures the entire string has a length between 3 and 16 characters (inclusive).

  2. First character must be a lowercase letter: [a-z]: The first character should be a lowercase letter.

  3. Following characters:

    • ([0-9a-z]: The second character should be either a digit or a lowercase letter.
    • |[0-9a-z\-](?=[0-9a-z]): Or, a digit, a lowercase letter, or a hyphen (-), followed by either a digit or a lowercase letter.
    • ){2,}: Repeat the above pattern at least twice.
  4. Optional subdomains:

    • ([\.](?=[a-z][0-9a-z\-][0-9a-z\-])[a-z]([0-9a-z]|[0-9a-z\-](?=[0-9a-z])){1,}): This matches a period (\.) followed by a lowercase letter and two characters, each of which can be a digit, a lowercase letter, or a hyphen (-). This pattern must occur at least once.
    • {0,}$: This makes the entire subdomain part optional.

In summary, this regex pattern is designed to match valid domain names, allowing for subdomains and enforcing specific rules for the characters that can be used in each part of the domain name."

Regards
Pi.ai

Hark, the evil one has returned to bless us with his black magic.

Here's a clue (from Co-Pilot)

The mystery_regex is a regular expression pattern used to validate a string. Let's break it down:

^(?!string$): This is a negative lookahead assertion that checks if the string is not exactly "string". If the string is exactly "string", it will not match the pattern.

(?=.{3,16}$): This is a positive lookahead assertion that checks if the string length is between 3 and 16 characters. If it's not, the string will not match the pattern.

[a-z]: This checks that the first character is a lowercase letter.

([0-9a-z]|[0-9a-z\-](?=[0-9a-z])){2,}: This checks that the following characters (at least 2) are either a lowercase letter, a number, or a hyphen, but a hyphen must be followed by a lowercase letter or a number.

([\.](?=[a-z][0-9a-z\-][0-9a-z\-])[a-z]([0-9a-z]|[0-9a-z\-](?=[0-9a-z])){1,}){0,}$: This checks that if there is a period in the string, it must be followed by a sequence that starts with a lowercase letter, followed by at least one character that is either a lowercase letter, a number, or a hyphen (but a hyphen must be followed by a lowercase letter or a number). There can be zero or more such sequences in the string.

In summary, this regular expression pattern is used to validate a string that:

Is not exactly "string"
Is between 3 and 16 characters long
Starts with a lowercase letter
Is followed by at least two characters that are either a lowercase letter, a number, or a hyphen (but a hyphen must be followed by a lowercase letter or a number)
If it contains a period, the period must be followed by a sequence that starts with a lowercase letter, followed by at least one character that is either a lowercase letter, a number, or a hyphen (but a hyphen must be followed by a lowercase letter or a number). There can be zero or more such sequences in the string.

Unsurprisingly it got a bit easier when I was told that r"" isn't part of the regex.

Yeah that's just a python syntax wrapper on the thing.

This one is good but let's see if you can figure out this one:

Never gonna give you up
Never gonna let you down
Never gonna run around and desert you
Never gonna make you cry
Never gonna say goodbye
Never gonna tell a lie and hurt you

Alright I actually went through it a bit just for the practice and it seems like a huge troll.

  • Right off the bat you don't give me the global modifiers which is a problem.
  • The first line has ^ which is the beginning of the line, but the beginning of the line starts with r". Even just typing r"^ into regex I couldn't figure out how to match it. Troll.
  • Then in the second line you're doing a lookahead of 3 alphanumeric characters but then when the lookahead retraces you're looking for 2 alphanumeric characters and a ". The third character can't be " because it was already confirmed to be not ". Again troll.

Because both the first and second lines don't even make sense and I can't match them this is where I quit. Although I thank you for showing me what positive and negative lookups are that is quite useful.

Can't wait for you to tell me how we're supposed to match r" and have that occur before the beginning of a string.

image.png

The guys in the hackMUD discord pointed out that r"" means raw and is probably a python script of 3 regex values in a tuple.

This is very troll sir. :D