Skip to content

Instantly share code, notes, and snippets.

@chartjes
Last active December 22, 2015 20:43
Show Gist options
  • Save chartjes/5acf166de83f1e8ef776 to your computer and use it in GitHub Desktop.
Save chartjes/5acf166de83f1e8ef776 to your computer and use it in GitHub Desktop.
Regex challenge
I have three strings
/kinto_read-only_1
/kinto_read-only_1/db1
/kinto_read-only_1/db1_1
* I want matches only on the first string
* the digit after a _ could be any number (ie only_1, only_2, only_3)
* the number after db1 could be any number (ie db1, db2)
Need a pattern to match this using Python's PRE functions
@steveklabnik
Copy link

If you want to match only the first string, why the requirement on the rest of the parts after it? or do you mean you want to catpure the first part

@chartjes
Copy link
Author

(Updated because I made a mistake)

Yeah, I only want matching the first string to be true

@ircmaxell
Copy link

what are the rules that cause the first to be true? No "/" characters after the "_"???

@loonytoons
Copy link

So not something as simple as ^/(kinto_read-only_\d+)?

@Ikke
Copy link

Ikke commented Dec 22, 2015

re.match("^/kinto_read-only_\d+$", "/kinto_read-only_1") for completeness (with the ending $).

@kirkbushell
Copy link

^ that one.

@sprak3000
Copy link

If you don't care about matching the exact kinto... string, you could generalize Ikke's answer down to ^/[^/]+$

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