I'm still quite unfamiliar with Python, albeit I have quite a bit of experience with JavaScript, so it's really only the idiosyncrasies of Python that I need to work on. Considering that, and the fact that I know there are some subtle differences between JS RegEx and Python RegEx, I have a question about a Python RegEx statement. Is there any way to make the following statement more concise?
The Whole Regular Expression
^https://www.indiegogo.com/explore/[a-z]+-?[a-z]+\?project_type=[a-z]+&project_timing=[a-z]+_?[a-z]+&tags=&sort=trending$
^https://www.indiegogo.com/explore/[a-z]+-?[a-z]+\?project_type=[a-z]+&project_timing=[a-z]+_?[a-z]+&tags=&sort=trending$
Breakdown of the Whole Regular Expression
I'll break this down further for you. The URL address will always begin with ^https://www.indiegogo.com/explore/
and always end with &tags=&sort=trending$
, so no need to worry about this, but...
[a-z]+-?[a-z]+\?project_type=[a-z]+&project_timing=[a-z]+_?[a-z]+
...is the specific part of the regular expression that matters, which can be broken down even further.
URL Structure and Possible Formats of Dynamic Values
^https://www.indiegogo.com/explore/
word
or dash-separated
or separated-by-dashes
or words-separated-by-dashes
?project_type=
word
&project_timing=
word
or additional_word
&tags=&sort=trending$
Steps 1.
, 3.
, 5.
, and 7.
can be ignored altogether, which leaves us with...
The Only Dynamic Values
2. word
or dash-separated
or separated-by-dashes
or words-separated-by-dashes
6. word
or additional_word
It may be my own ignorance or inexperience, but the regular expression I've devised seems clunky so to speak. Is there any way to improve this regular expression?!
Without having any sample URLs to test with, the simplest solution I could find is this:
^https:\/\/www.indiegogo.com\/explore\/[a-z\-?_=]+&project_timing=[a-z_]+&tags=&sort=trending$
So here's a breakdown of what I did differently:
[a-z]+-?[a-z]+\?project_type=[a-z]+
, I simplified it with [a-z\-?_=]+
[a-z]+_?[a-z]+
I used [a-z_]+
The only issue I saw was that you aren't taking full advantage of your character classes. If you would like to provide a few sample URLs I'd be able to fix any issues you might run into. But as far as I can tell, it does what you need it to.
You can also use ^https:\/\/www.indiegogo.com\/explore\/[\w\-]+&project_timing=[\w]+&tags=&sort=trending$
if you really want to simplify, but that might not be restrictive enough.
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加