Skip to content
Advertisement

How to target multiple strings with single regex pattern

I have multiple strings such as

JavaScript

I want to target all these strings with regex.

I tried the following pattern

JavaScript

Here is the full code

JavaScript

This outputs the following

JavaScript

With this pattern, I am able to target the first three strings. But for the life of me, I am not able to figure out how to target the last string. This is just a sample of many more strings in the list. I need to make this dynamic so that the program is able to capture strings that are similar to this.

I am a rookie in python and have just started learning regex.

Any help will be appreciated.

Advertisement

Answer

I would use re.findall here with the following regex pattern:

JavaScript

Script:

JavaScript

This prints:

JavaScript

The regex pattern works by matching one of several HTTP methods in an alternation, to which you may add more methods if necessary. Then, it matches a path, followed by HTTP and a version number.

Advertisement