mbeals Posted November 12, 2007 Share Posted November 12, 2007 I'm writing a script to strip out data from incoming e-mails that are of a standard format. If I have a line that reads: . Name: Foo Bar<br /> I need to extract just the "Foo Bar" part. I'm using this regex: [Name:\s.+\W], which keys off the "Name" and "<". It works fantastic, except it returns "Name: Foo Bar"..... I don't want the leading "Name: ". How do you structure it so that it searches for but does not return that opening tag? I also suspect it is returning the < character, but it's not showing up in any of the output (CLI or web). Quote Link to comment https://forums.phpfreaks.com/topic/77018-delimited-text/ Share on other sites More sharing options...
effigy Posted November 12, 2007 Share Posted November 12, 2007 Are you using [ and ] as your delimiters? Capture the content: /Name:\s+(.+)/ Quote Link to comment https://forums.phpfreaks.com/topic/77018-delimited-text/#findComment-390003 Share on other sites More sharing options...
mbeals Posted November 12, 2007 Author Share Posted November 12, 2007 Thanks, that worked perfectly Quote Link to comment https://forums.phpfreaks.com/topic/77018-delimited-text/#findComment-390024 Share on other sites More sharing options...
mbeals Posted November 12, 2007 Author Share Posted November 12, 2007 okay....new unforseen issue. When one of the fields has no text, it looks like: Alt Phone:<br /> The regex I'm using us dropping down to the next line and grabbing that full string. So for this text block: Alt Phone:<br /> Name: Foo Bar<br /> The regex is returning: Name: Foo Bar Quote Link to comment https://forums.phpfreaks.com/topic/77018-delimited-text/#findComment-390055 Share on other sites More sharing options...
obsidian Posted November 12, 2007 Share Posted November 12, 2007 Why not post your full loop code? If you are pulling from an external file, you could easily get around this by simply reading the file line by line. If you are reading this from a variable, be sure that you don't have the multi-line match flag turned on in your regexp match. Quote Link to comment https://forums.phpfreaks.com/topic/77018-delimited-text/#findComment-390059 Share on other sites More sharing options...
mbeals Posted November 12, 2007 Author Share Posted November 12, 2007 I'm using the mailparse extensions, so it opens up the /var/mail/$user mail file then runs a regex search the exact code in question is: preg_match_all('/Phone:\s*(.*)/', $contents, $altphone); preg_match_all('/Name:\s+(.+)/', $contents, $names); I'd prefer not to pull it in line by line and to just let mailparse handle the input side of things. Quote Link to comment https://forums.phpfreaks.com/topic/77018-delimited-text/#findComment-390067 Share on other sites More sharing options...
mbeals Posted November 12, 2007 Author Share Posted November 12, 2007 I'm sorry, I forgot to use the code tags and consequently a big piece of info was left out. The source file looks like this: Alt Phone:<br /> Name: Foo Bar<br /> Not like: Alt Phone: Name: Foo Bar So I'm attempting to pull out everything between the : and the < Quote Link to comment https://forums.phpfreaks.com/topic/77018-delimited-text/#findComment-390078 Share on other sites More sharing options...
effigy Posted November 12, 2007 Share Posted November 12, 2007 How about /Name:\x20+([^<]*)/? Quote Link to comment https://forums.phpfreaks.com/topic/77018-delimited-text/#findComment-390081 Share on other sites More sharing options...
mbeals Posted November 12, 2007 Author Share Posted November 12, 2007 How about /Name:\x20+([^<]*)/? that ends up pulling in the entire remainder of the email I think I resolved it. I'm just using /Phone:(.*)/ It does capture the leading space when there is data, but that's not a big deal. thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/77018-delimited-text/#findComment-390085 Share on other sites More sharing options...
effigy Posted November 12, 2007 Share Posted November 12, 2007 [^<\r\n]* Quote Link to comment https://forums.phpfreaks.com/topic/77018-delimited-text/#findComment-390114 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.