Question: I have the following Strings `B113AAA`, `SV54DDD`. I want to test any String based on this format `[LETTER{LETTER}][NUMBERNUMBER{NUMBER}][LETTERLETTERLETTER]`. 

I put the second `{letter}` and third `{number}` because it is optional. I want to know if I can use regex to do this without any other condition statements(if). If the fisrt LETTER is `B` for example, there is no need for the second letter in `{}`, and if it is `B` I can have 3 Numbers in the middle. But if it is anything else than `B`, it must have 2 letter and 2 numbers ( `LETTERLETTER NUMBERNUMBER LETTER LETTER LETTER`). 
Thank you!

This works only for `BB1N`, for `B1N` doesnt work.

```
String x ="B1N";
if(x.matches("[A-Z]{2}[0-9][A-Z]"))

```

Answer: 
No need for Regex. You can do this with string methods `split` and `partition`:

```
In [181]: srcset = "https://cimg.co/w/articles/1/5ca/f022bb06dc.png 150w, https://cimg.co/w/articles/2/5ca/f022bb06dc.png 300w, https://cimg.co/w/articles/3/5ca/f022bb06dc.png 600w, https://cimg.co/w/arti
     ...: cles/4/5ca/f022bb06dc.png 1200w"                                                                                                                                                                  

In [182]: def get_url(srcset): 
     ...:     for str_ in srcset.split(','): 
     ...:         url, _, ext = str_.strip().partition(' ') 
     ...:         if ext == '1200w': 
     ...:             return url 
     ...:                                                                                                                                                                                                   

In [183]: get_url(srcset)                                                                                                                                                                                   
Out[183]: 'https://cimg.co/w/articles/4/5ca/f022bb06dc.png'

```

Assuming `,` does not come in URLs.

---

If you must use Regex, you can do:

```
https?://\S+(?=\s+1200w\b)

```

So:

```
In [184]: re.search(r'https?://\S+(?=\s+1200w\b)', srcset).group()                                                                                                                                          
Out[184]: 'https://cimg.co/w/articles/4/5ca/f022bb06dc.png'

```

* `https?://\S+` matches the URL
* The zero-width postive lookahead, `(?=\s+1200w\b)`, ensures that the URL is followed by one or more spaces(`\s+`), then `1200w`

OTOH, if you don't feel comfortable with the HTTP scheme based matching, you can match the start or `,`, and grab the first captured group:

```
In [185]: re.search(r'(?:^|,\s+)(\S+)\s+1200w\b', srcset).group(1)                                                                                                                                          
Out[185]: 'https://cimg.co/w/articles/4/5ca/f022bb06dc.png'

```
The pattern `.+?(?=1200w)` will match any character except a newline 1+ times until what is on the right is `1200`.

To get a more specific match using a regex, you could use a capturing group:

```
\bsrcset="[^"]* (https?://\S+)\s+1200w"

```

[Regex demo](https://regex101.com/r/xfaOS1/1) | [Python demo](https://ideone.com/Yq9wD7)

For example:

```
import re
regex = r'\bsrcset="[^"]* (https?://\S+)\s+1200w"'
test_str = """srcset=\"https://cimg.co/w/articles/1/5ca/f022bb06dc.png 150w, https://cimg.co/w/articles/2/5ca/f022bb06dc.png 300w, https://cimg.co/w/articles/3/5ca/f022bb06dc.png 600w, https://cimg.co/w/articles/4/5ca/f022bb06dc.png 1200w\""""

matches = re.search(regex, test_str)
if matches:
    print(matches.group(1))

```

Result

```
https://cimg.co/w/articles/4/5ca/f022bb06dc.png

```