1

I have a field which contain 20 character (pad string with space character from right) like below:

VINEYARD HAVEN MA
BOLIVAR TN
,
BOLIVAR, TN
NORTH TONAWANDA, NY

How can I use regular expression to parse and get data, the result I want will look like this:

[1] VINEYARD HAVEN [2] MA

[1] BOLIVAR [2] TN

[1] , or empty [2] , or empty

[1] BOLIVAR, or BOLIVAR [2] TN or ,TN

[1] NORTH TONAWANDA, or NORTH TONAWANDA [2] NY or ,NY

Currently I use this regex:

^(\D*)(?=[ ]\w{2}[ ]*)([ ]\w{2}[ ]*)

But it couldnot match the line:

,

Please help to adjust my regex so that I match all data above

1
  • it's not very clear what do you want to achieve :( Commented Feb 4, 2016 at 9:34

2 Answers 2

1

What about this regex: ^(.*)[ ,](\w*)$ ? You can see working it here: http://regexr.com/3cno7.


Example usage:

<?php

$string = 'VINEYARD HAVEN MA
BOLIVAR TN
,
BOLIVAR, TN
NORTH TONAWANDA, NY';

$lines = array_map('trim', explode("\n", $string));

$pattern = '/^(.*)[ ,](\w*)$/';

foreach ($lines as $line) {
    $res = preg_match($pattern, $line, $matched);

    print 'first: "' . $matched[1] . '", second: "' . $matched[2] . '"' . PHP_EOL;
}
Sign up to request clarification or add additional context in comments.

3 Comments

just test your regex, it match the ", " line but it didn't parse data for other lines :)
Here is example working code: sandbox.onlinephpfunctions.com/code/… . How did you use it?
Site has some maintenance, I added that code to my answer.
0

It's probably possible to implement this in a regular expression (try /(.*)\b([A-Z][A-Z])$/ ), however if you don't know how to write the regular expression you'll never be able to debug it. Yes, its worth finding out as a learning exercise, but since we're talking about PHP here (which does have a mechanism for storing compiled REs and isn't often used for bulk data operations) I would use something like the following if I needed to solve the problem quickly and in maintainable code:

$str=trim($str);
if (preg_match("/\b[A-Z][A-Z]$/i", $str, $match)) {
    $state=$match[0];
    $town=trim(substr($str,0,-2)), " ,\t\n\r\0\x0B");
}

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.