0

Need help with a regular expression.

1) Format:

 Advisor Geologist – HighMount Exploration & Production LLC – Oklahoma City, OK

I'd like to get the text between the last character and the dash. ie. Oklahoma City, OK. Note the text might contain multiple dashes.

Tried this:

~-([.*$]+)~

Trying to get between the dash and the end of the string (.*$). Need to know how to check for the last occurrence of the dash.

5
  • 6
    And what have you tried? Commented Nov 19, 2012 at 16:55
  • 2
    Show us what you've tried, so we have a starting point to work with. Commented Nov 19, 2012 at 16:56
  • Have you tried anythig? See php.net/manual/en/reference.pcre.pattern.syntax.php to get started with regex in PHP. Commented Nov 19, 2012 at 16:59
  • Sorry have updated my question Commented Nov 19, 2012 at 16:59
  • @m.buettner - You don't know why OP asks for regex solution - maybe (s)he needs regex and any non-regex solution is then worthless! Commented Nov 19, 2012 at 17:19

4 Answers 4

8

You don't need a regular expression, explode() the string on the dash, and take the last element.

$str = 'Advisor Geologist – HighMount Exploration & Production LLC – Oklahoma City, OK';
$arr = explode( '–', $str);
$last = trim( end( $arr));
echo $last;

Much more efficient.

Sign up to request clarification or add additional context in comments.

6 Comments

How you know OP does not need regex? You don't see whole picture of the issue (s)he is working on... Sometimes users need regex, because they use some tool or library that works with regex, not the actual PHP code...
The whole picture of the issue is depicted in the OP, unless I'm supposed to be a mind reader?
Your solution is excellent, if OP does not need regex. You should offer a regex alternative as well - that is my point...
This would be a good solution but please note that those characters in $str aren't dashes, they are something else.
Didn't specifically need a regex. Couldn't think of a solution that wasn't a regex. This has worked great. Wasn't aware of the end() so Thanks!
|
1

If you need to use regex, then go with

$pattern = '/[^\s–-][^–-]*?(?=\s*$)/';
preg_match($pattern, $subject, $matches);

Test this demo here.

5 Comments

(in reply to your comment on the question) sure, and I didn't downvote your solution because it used a regex, but because it did not fulfill the specification stated in the question. While I still prefer the the non-regex solution, this one gets my upvote, because (for some odd reason) the OP might actually need a regex. And this one is working pretty well ;) (as opposed to the last one)
@BotondBalázs Ωmega had posted a different solution before, which is the one I was referring to
Tried this solution but it when I var_dump() $matches I just get the whole string. The string I passed in was "VP of Exploration - Global Recruiters of Dallas - Houston, TX"
@iamjonesy that was my fault then. I adjusted the regex to the dash- characters you used in the question (which are not - but ). Adjusted it again so that it work with both characters. Try again please
@m.buettner: sorry then, I totally misunderstood it. I'll delete my comment.
0

If you need a regex, this is the simplest one I can think of:

'/\s*([^-]+)\s*$/'

Let's see how it works:

  • \s* matches zero or more whitespace characters (spaces, tabs, etc.)
  • ([^-]+) matches one or more characters that are not dashes
  • \s* matches zero or more whitespace characters (spaces, tabs, etc.)
  • $ matches the end of the string

Please note that what the character in your post is not a simple dash. It is some other Unicode character. If you need to match that too, you should update the regex this way:

'/\s*([^-–]+)\s*$/'

Here is a code sample:

preg_match(
    '/\s*([^-]+)\s*$/',
    'Advisor Geologist – HighMount Exploration & Production LLC - Oklahoma City, OK',
    $matches);
$city = $matches[1];

1 Comment

With this pattern you would need to trim spaces then... Also, no need to escape - inside of [...] if - is first, last or only one in...
0

Also strrpos() can help.

 $str = 'Advisor Geologist – HighMount Exploration & Production LLC – Oklahoma City, OK';
 $result = trim(substr($str, strrpos($str, '-')+1));

For fixed formats you can use list() & explode():

 $str = 'Advisor Geologist – HighMount Exploration & Production LLC – Oklahoma City, OK';
 list($occupation, $company, $city) = explode('-', $str);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.