简体   繁体   English

PHP正则表达式-提取数据

[英]PHP Regular Expression - Extract Data

I have a long string, and am trying to extract specific data that is deliminated in that string by specific words. 我有一个长字符串,并且正在尝试提取由特定单词在该字符串中定义的特定数据。

For example, here is a subset of the string: 例如,这是字符串的子集:

Current   Owner  123 Capital    Calculated  

I am looking to extract 我正在寻找提取

123 Capital

and as you can see it is surrounded by "Current Owner" (with a bunch of arbitrary spaces) to the left and "Calculated" (again with arbitrary spaces) to the right. 如您所见,它的左边是“当前所有者”(带有一堆任意空格),右边是“已计算”(同样是带有任意空格)。

I tried this, but I'm a bit new at RegEx. 我试过了,但是RegEx有点新。 Can anyone help me create a more effective RegEx? 谁能帮助我创建更有效的RegEx?

preg_match("/Owner[.+]Calculated/",$inputString,$owner);

Thanks! 谢谢!

A character class defines a set of characters. 字符类定义一组字符。 Saying, "match one character specified by the class". 说“匹配类指定的一个字符”。 Place the dot . 放置点. and quantifier inside of a capturing group instead and enable the s modifier which forces the dot to span newlines. 取而代之的是在捕获组内添加量词,并启用s修饰符,该修饰符可强制点跨越换行符。

preg_match('/Owner(.+?)Calculated/s', $inputString, $owner);
echo trim($owner[1]);

Note: + is a greedy operator, meaning it will match as much as it can and still allow the remainder of the regex to match. 注意: +贪婪的运算符,表示它将尽可能匹配,并且仍然允许正则表达式的其余部分匹配。 Use +? 使用+? instead to prevent greediness meaning "one or more — preferably as few as possible". 相反,为了防止贪婪,意思是“一个或多个-最好尽可能少”。

You can use lookarounds as 您可以使用环顾四周

(?<=Owner)\s*.*?(?=\s+Calculated)

Example usage 用法示例

$str = "Current Owner 123 Capital Calculated ";
preg_match("/(?<=Owner)\s*.*?(?=\s+Calculated)/", $str, $matches);
print_r($matches);

Will give an output 将给出输出

Array ( [0] => 123 Capital ) 

Hope this helps, group index #1 is your target: 希望对您有所帮助,组索引1是您的目标:

Owner\\s+(\\d+\\s+\\w+)\\s+Calculated

You may also want to try a tool like RegExr to help you learn/tinker. 您可能还想尝试使用RegExr之类的工具来帮助您学习/修补。

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

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