/    Sign up×
Community /Pin to ProfileBookmark

need help regex trouble

I have a few DB records that look line this

John Doe
Fred Smith FT
Mickey Mouse ERR more stuff

what I need is to split these records up On the part that has more than 2 capital letters

John Doe
Fred Smith, FT
Mickey Mouse, ERR more stuff

problem is that
(.+)s*([A-Z]{2,}.*)
works if the 2nd part exists, but does not if there are not 2 capital letters in the sequence. Anyone have any ideas on how to take care of this?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@sneakyimpDec 08.2008 — You need to be more clear about your problem.
Copy linkTweet thisAlerts:
@DARTHTAMPONauthorDec 08.2008 — I have this list

John Doe

Fred Smith FT

Mickey Mouse ERR more stuff

John Jacob Heimer AARP

I need to turn it into an array that looks like this

array("John Doe","")

array("Fred Smith", "FT")

array("Mickey Mouse", "ERR more stuff")

array("John Jacob Heimer", "AARP")

The 2nd part of the array is determined by the occurance of 2 or more capital letters in sequence. Anything that come after needs to be included. Items that do not have 2 capital letters in sequence still need to match the regex, but leave the 2nd group blank.
Copy linkTweet thisAlerts:
@sneakyimpDec 08.2008 — Not sure if this will work in every case, but it seems to be working for me:
[code=php]
$arr[] = "John Doe";
$arr[] = "Fred Smith FT";
$arr[] = "Mickey Mouse ERR more stuff";
$arr[] = "John Jacob Heimer AARP";


$pattern = '/^(.+)s+([A-Z]{2})(.*)$/';
$new_arr = array();
foreach ($arr as $key => $str) {
$matches = array();
if (!preg_match($pattern, $str, $matches)) {
echo $new_arr[] = array($str, "");
} else {
array_shift($matches);
$pt1 = array_shift($matches);
$pt2 = implode('', $matches);
$new_arr[] = array($pt1, $pt2);
}
}
print_r($new_arr);
[/code]


You said you had a 'list' so I wasn't exactly sure whether that would be an array or whether it would be a text file or whatever.
×

Success!

Help @DARTHTAMPON spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...