简体   繁体   English

正则表达式 - 如何在第一次匹配之前捕获?

[英]Regular Expression - How do I capture before the first match?

Apparel & Fashion · United States · 10001+ employees · 5 billion Revenue服装与时尚 · 美国 · 10001+ 员工 · 50 亿收入

Non-profit Organization · Canada · 501-1000 employees非营利组织 · 加拿大 · 501-1000 名员工

Here are a couple examples above.这是上面的几个例子。 I am trying to capture the industry ( ex. Apparel & Fashion or Non-Profit Organization )我试图抓住这个行业(例如Apparel & FashionNon-Profit Organization

I am trying to use the first · as a capture reference我正在尝试使用第一个 · 作为捕获参考

This captures everything before the last dot (.+)\s·这将捕获最后一个点 (.+)\s·之前的所有内容

But i want to capture everything before the first dot.但我想在第一个点之前捕获所有内容。 How do I do this?我该怎么做呢? Sorry that this is a basic question.抱歉,这是一个基本问题。

Capture everything that is NOT the · .捕获所有不是·的东西。 This allows you to use preg_match_all if needed to get all of the parts delimited by · :这允许您在需要时使用preg_match_all来获取由·分隔的所有部分:

([^·]+)

You can add the beginning anchor to only get the first one, but probably not needed:您可以添加开始锚点以仅获取第一个锚点,但可能不需要:

^([^·]+)

However in PHP just:但是在 PHP 中:

$result = explode('·', $text)[0];

Or to get all:或者得到所有:

$results = explode('·', $text)[0];
echo $results[0];
echo $results[1];
//etc...

You should use non-greedy regexp.您应该使用非贪婪的正则表达式。 (.+?)· (.+?)·

No need for regular expressions at all, see this code:完全不需要正则表达式,看这段代码:

$position = strpos($string, "·", 5);  
$string = substr ( $string , 0, $position - 2 );

Where $string is the string to parse其中$string是要解析的字符串

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

相关问题 如何编写仅在匹配三个必需的捕获组时才匹配的正则表达式 - How do I write a regular expression that only matches if match three required capture groups 正则表达式在前%后和后%前匹配 - Regular expression to match after first % and before last % 正则表达式始终匹配第一个组“ first”(在其他组之前) - Regular expression to match always the first group 'first' (before) the other groups 如何为字符串创建正则表达式以匹配[@username:4]? - How do I create regular expression for a string to match [@username:4]? PHP:如何将正则表达式转换为示例匹配? - PHP: How do I convert a regular expression to an example match? 如何在Perl中进行全局正则表达式匹配? - How can I do a global regular expression match in Perl? 如何用正则表达式匹配所有内容? - How can I match everything with a regular expression? 我想使用正则表达式匹配字符串的第一个世界 - I want to match the first world of the string using regular expression 如何在 PHP 中使用与整个字符串中的给定组匹配的正则表达式,而不是在第一次匹配时停止 - How can i use a regular expression in PHP that matches a given group in the whole string instead of stops at first match PHP正则表达式,如何匹配包含[符号的第一部分? - PHP Regular expression, how to match the first section which includes a [ symbol?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM