
Spinner
Novice
Feb 5, 2011, 9:32 AM
Post #1 of 4
(1725 views)
|
|
Regex, multiple matches, loop
|
Can't Post
|
|
I have a string which can look like this: Lorem ipsum dolor sit amet, @John Doe@ consectetur adipiscing elit. Donec porta lorem @Jane Doe@ fermentum urna laoreet hendrerit. Maecenas @Mac Bob@ nisi mi, rutrum nec luctus at, tincidunt non eros. What I want it to look like is simply Lorem ipsum dolor sit amet, [name]12345:John Doe[/name] consectetur adipiscing elit. Donec porta lorem [name]23456:Jane Doe[/name] fermentum urna laoreet hendrerit. Maecenas [name]34567:Mac Bob[/name] nisi mi, rutrum nec luctus at, tincidunt non eros. For any lazy folks out there, I have replaced @<data>@ with [name]12345:<data>[/name] . Now, the regex for doing that switch is of course not a problem. The problem is that I do now know the numbers that I want to insert before I have "processed" the match itself (checking the name against a database and then finding the number I need to put in there, which is not a problem in itself). So how would I go about doing something like this: Find all matches, then, for each match, look up the name in the DB to find the number, and then replace the match with my altered string? Is there a way a matching-regex can give me multiple results in an array, so to speak, for traversing? Any help would be much appreciated. - Spinner
|