[英]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 & Fashion
或Non-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.