-1

I have next html code:

<table>
                <tr><td><input type="radio" name="download" value="https://cdn.pixabay.com/photo/2018/04/17/00/42/water-3326310_640.jpg"> 640×425</td><td>JPG</td><td>48 kB</td></tr>

                    <tr><td><input type="radio" name="download" value="water-3326310_1280.jpg" data-perm="check"> 1280×851</td><td>JPG</td><td>158 kB</td></tr>


                    <tr><td><input type="radio" name="download" value="water-3326310_1920.jpg" data-perm="check"> 1920×1277</td><td>JPG</td><td>335 kB</td></tr>

                <tr class="no_default"><td><input type="radio" name="download" value="water-3326310.jpg" data-perm="auth"> 4256×2832</td><td>JPG</td><td>2.1 MB</td></tr>

            </table>

I know resolution and now I need to get value based on resolution. On example for 1920×1277 it would be water-3326310_1920.jpg

How I can do this with regex, I never had need to select value in backward. I tried this:

Regex n2 = new Regex(@"value=""(.+?) 1920×1279");

But the problem is that we have multiple value="" and it will start from the first one which makes it impossible to recognize the right value.

BTW HTML is in one line since I did:

string i = Regex.Replace(x, "(?:\r?\n|\r)+", "");

It's easier to do regex in one line.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Demir Karic
  • 147
  • 11

2 Answers2

0

After the obligatory don't use Regex to parse HTML, your pattern is close.

Regex n2 = new Regex(@"value=""(.+_1920\.[^""]+)""");

You can use it like this:

var m = n2.Match(src);
if (m.Success)
    filename = m.Groups[1].Value;

If you really need to use the full resolution (PS I noticed it is a special character in there), you can use:

Regex n2 = new Regex(@"value=""([^""]+)""(?=.+?>1920×1277)");
NetMage
  • 26,163
  • 3
  • 34
  • 55
0

You could do:

(?<=value=")[^"]*(?=.*1920×1277)

This will match values only when there is a 1920x1277 string anywhere after the value, in the same line.

builder-7000
  • 7,131
  • 3
  • 19
  • 43