1

I parse an html page into a plain text in order to find and get a numeric value. In the whole html mess, I need to find a string like this one:

C) Debiti33.197.431,90I - Di finanziamento

I need the number 33.197.431,90 (where this number is going to change on every html parsing request.

Is there any regex to achieve this? For example:

STARTS WITH 'C) Debiti' ENDS WITH 'I - Di finanziamento' GETS the middle string that can be whatever.

Whenever I try, I get empty results...don't know that much about regex. Can you please help me? Thank you very much.

2 Answers 2

3

You could try the below regex,

^C\) Debiti\K.*?(?=I - Di finanziamento$)

DEMO

PHP code would be,

<?php
$mystring = "C) Debiti33.197.431,90I - Di finanziamento";
$regex = '~^C\) Debiti\K.*?(?=I - Di finanziamento$)~';
if (preg_match($regex, $mystring, $m)) {
    $yourmatch = $m[0]; 
    echo $yourmatch;
    }
?> //=> 33.197.431,90
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you. With your regex I get the beginning and the end of the string, but not the number I am looking for. What am I missing?
it works for me. see the above code. You may just copy and paste the above code in a file and then run it.
thanks! I managed to get it working before your code, but any way, thank you again. What still remains unsolved is why if the string is just "C) Debiti33.197.431,90I - Di finanziamento", it works perfectly, but if I do a whole search in a long webpage, where that string is contained, I get a white page (no results).
if C) Debiti33.197.431,90I present at the middle of a page then it won't work because we already gave start and end patterns in the original pattern.
Oh...that's bad news. I could extract the string "C) Debiti33.197.431,90I - Di finanziamento" from the whole html, couldn't I? Maybe deleting everything is before the words "C) Debiti" and after the words "I - Di finanziamento". The problem is...how :(
|
0

This should work. Read section Want to Be Lazy? Think Twice.

(?<=\bC\) Debiti)[\d.,]+(?=I - Di finanziamento\b)

Here is demo

sample code:

$re = "/(?<=\\bC\\) Debiti)[\\d.,]+(?=I - Di finanziamento\\b)/i";
$str = "C) Debiti33.197.431,90I - Di finanziamento";

preg_match($re, $str, $matches);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.