Make Python RegEx More Concise

oldboy

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

  1. ^https://www.indiegogo.com/explore/
  2. word or dash-separated or separated-by-dashes or words-separated-by-dashes
  3. ?project_type=
  4. word
  5. &project_timing=
  6. word or additional_word
  7. &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?!

emsimpson92

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:

  • Instead of [a-z]+-?[a-z]+\?project_type=[a-z]+, I simplified it with [a-z\-?_=]+
  • Instead of [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]

編集
0

コメントを追加

0

関連記事

分類Dev

Writing more concise code in go

分類Dev

Is there a more concise way to run this query in SQL?

分類Dev

More concise way of making quadratically spaced sequence?

分類Dev

Is there a more concise way to initialize a unique_ptr<char[]> than this?

分類Dev

Getting regex to be more leniant

分類Dev

Is there a more concise method to filter columns containing a number greater than or less than, in dplyr?

分類Dev

Is there any ways to make this more efficient?

分類Dev

Make string checker more efficient

分類Dev

Make a conditional regex

分類Dev

Does there exist a concise regex that has the same effect as (0|1)*(1100|1010|1001|0110|0101|0011)?

分類Dev

Android Studio and regex search (find more words)

分類Dev

Regex replacing more than I want

分類Dev

Regex match sequence more than once

分類Dev

Regex: two or more consecutive characters or character sequences

分類Dev

How to make a character required in regex?

分類Dev

Are there example layouts to make doxygen documentation more appealing?

分類Dev

How to make more readable and shorter a karate scenario

分類Dev

Make code less complex and more readable

分類Dev

How to group props and make code more readable?

分類Dev

Is there a way to make this switch statement smaller and more professional?

分類Dev

DT & shiny: make header borders more pronounced

分類Dev

Need to make excel vba vlookup more efficient

分類Dev

How can I make the button more visible?

分類Dev

How to make the switch statement more accurate?

分類Dev

How to make taskbar more transparent in Windows 10

分類Dev

How to make JsHint sbt plugin more useful?

分類Dev

Protractor: how to make the configuration file more flexible?

分類Dev

Python split() return more characters

分類Dev

Python child class with more methods

Related 関連記事

  1. 1

    Writing more concise code in go

  2. 2

    Is there a more concise way to run this query in SQL?

  3. 3

    More concise way of making quadratically spaced sequence?

  4. 4

    Is there a more concise way to initialize a unique_ptr<char[]> than this?

  5. 5

    Getting regex to be more leniant

  6. 6

    Is there a more concise method to filter columns containing a number greater than or less than, in dplyr?

  7. 7

    Is there any ways to make this more efficient?

  8. 8

    Make string checker more efficient

  9. 9

    Make a conditional regex

  10. 10

    Does there exist a concise regex that has the same effect as (0|1)*(1100|1010|1001|0110|0101|0011)?

  11. 11

    Android Studio and regex search (find more words)

  12. 12

    Regex replacing more than I want

  13. 13

    Regex match sequence more than once

  14. 14

    Regex: two or more consecutive characters or character sequences

  15. 15

    How to make a character required in regex?

  16. 16

    Are there example layouts to make doxygen documentation more appealing?

  17. 17

    How to make more readable and shorter a karate scenario

  18. 18

    Make code less complex and more readable

  19. 19

    How to group props and make code more readable?

  20. 20

    Is there a way to make this switch statement smaller and more professional?

  21. 21

    DT & shiny: make header borders more pronounced

  22. 22

    Need to make excel vba vlookup more efficient

  23. 23

    How can I make the button more visible?

  24. 24

    How to make the switch statement more accurate?

  25. 25

    How to make taskbar more transparent in Windows 10

  26. 26

    How to make JsHint sbt plugin more useful?

  27. 27

    Protractor: how to make the configuration file more flexible?

  28. 28

    Python split() return more characters

  29. 29

    Python child class with more methods

ホットタグ

アーカイブ