简体   繁体   English

PHP正则表达式获取字符串的中间

[英]php regex to get middle of string

I parse an html page into a plain text in order to find and get a numeric value. 我将html页面解析为纯文本,以查找并获取数字值。 In the whole html mess, I need to find a string like this one: 在整个html混乱中,我需要找到一个像这样的字符串:

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. 我需要数字33.197.431,90(其中每个HTML解析请求中的数字都会改变)。

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. 非常感谢你。

You could try the below regex, 您可以尝试以下正则表达式,

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

DEMO 演示

PHP code would be, PHP代码是

<?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

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM