简体   繁体   中英

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

Apparel & Fashion · United States · 10001+ employees · 5 billion Revenue

Non-profit Organization · Canada · 501-1000 employees

Here are a couple examples above. I am trying to capture the industry ( ex. Apparel & Fashion or Non-Profit Organization )

I am trying to use the first · as a capture reference

This captures everything before the last dot (.+)\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 · :

([^·]+)

You can add the beginning anchor to only get the first one, but probably not needed:

^([^·]+)

However in PHP just:

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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