简体   繁体   English

正则表达式语法在PHP中提取格式的所有实例

[英]Regular expression syntax In PHP to extract all instances of a format

I am attempting to extract all instances of a particular format from a string: 我正在尝试从字符串中提取特定格式的所有实例:

I am wondering if my new Sony [PT# 123456ABC; 我想知道我的新Sony [PT#123456ABC; Sony] has this feature but my friend says the new Toshiba [PT# AD-3232hjk; 索尼]具有此功能,但我的朋友说新的东芝[PT#AD-3232hjk; Toshiba] has this feature. 东芝]具有此功能。

I would like to extract: 我想提取:

[PT# 123456ABC; [PT#123456ABC; Sony] 索尼]

[PT# AD-3232hjk; [PT#AD-3232hjk; Toshiba] 东芝]

As you can see here, the only items in the consistent positions are: 如您所见,处于一致位置的唯一项目是:

  • [PT# [PT#
  • ; ;
  • ] ]

I was attempting to use various types of strpos() but because of the varying lengths and formats of the part numbers and manufacturer names I was unable to reliably pull out those instances from a much larger string. 我试图使用各种类型的strpos(),但是由于部件号和制造商名称的长度和格式的不同,我无法可靠地从更大的字符串中提取那些实例。 I have been trying various ways to use regular expressions to solve this however my knowledge with them is fairly limited. 我一直在尝试各种使用正则表达式解决此问题的方法,但是我对它们的了解非常有限。 After I have these expressions extracted and placed into variables I will then need to separate the part numbers and manufacturer names from the expression. 将这些表达式提取并放入变量后,我将需要从表达式中分离零件号和制造商名称。 This may also be easier to accomplish using regular expressions. 使用正则表达式也可能更容易实现。

Any help is appreciated. 任何帮助表示赞赏。 Thanks 谢谢

$matches = array();
preg_match_all( "/\[PT#([^\];]+);([^\]]+)\]/", $input, $matches,  PREG_SET_ORDER);

foreach ($matches as $match) {
  echo "id=", trim($match[1]), " brand=", trim($match[2]), "\n";
}

I think this would do it 我认为这可以做到

preg_match_all( "/(\[PT#\s+.*?;\s+.*?\])/", $input, $matches );

print_r( $matches );

Altternatively, if you just wanted to capture the unique information 或者,如果您只想捕获唯一信息

preg_match_all( "/\[PT#\s+(.*?);\s+(.*?)\]/", $input, $matches );

I take it you'll be reading from a text file containing a lot of those entries. 我认为您将从包含许多这些条目的文本文件中读取。 What you can do is: 您可以做的是:

preg_match_all("/\[PT#(.*?);[.*]?(.*?)\]/i", $text, $result);

it will put all matches into the array $result and you can access them as so: 它将所有匹配项放入数组$ result中,您可以这样访问它们:

echo $result[1][0]; //echos first occurrence's serial

$result is sorted column major and the first entry into a match is the complete match string $ result按主列排序,匹配的第一个条目是完整的匹配字符串

echo $result[0][0]; // would print [PT# 123456ABC; Sony]
echo $result[1][0]; // would print 123456ABC
echo $result[2][0]; // would print Sony

Hope that helps 希望能有所帮助

EDIT: fixed the regex, should work now (still untested) 编辑:修复了正则表达式,现在应该可以使用(仍然未经测试)

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

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