Questions about builtin RegExp

Regan Heath regan at netwin.co.nz
Sun Feb 19 15:41:51 PST 2006


On Sun, 19 Feb 2006 14:47:43 -0800, Unknown W. Brackets  
<unknown at simplemachines.org> wrote:
> Andrew Fedoniouk,
>
> What he's saying is... essentially... please take this string:
>
> char[] some_text = "The email address Walter is posting from is  
> newshound at digitalmars.com.  The headers for your message have  
> <news at terrainformatica.com>, so I would assume that is your address.  My  
> address can be found in this HTML: <a  
> href=\"mailto:unknown at simplemachines.org\">my email</a>";
>
> Now use strtok to output just the email addresses.  I would expect the  
> output to be like this:
>
> 1: newshound at digitalmars.com
> 2: news at terrainformatica.com
> 3: unknown at simplemachines.org
>
> How many lines will it take to grab those addresses, without using a  
> regular expression?  You can use "like()" all you like, and strtok(), or  
> even strpos()...

Here's how I'd do it:

import std.stdio;
import std.string;

char[] some_text = "The email address Walter is posting from is  
newshound at digitalmars.com.  The headers for your message have  
<news at terrainformatica.com>, so I would assume that is your address.  My  
address can be found in this HTML: <a  
href=\"mailto:unknown at simplemachines.org\">my email</a>";

void main()
{
	char[][] res;	
	res = parse_string(some_text);
	foreach(int i, char[] r; res)
		writefln("%d. %s",i+1,r);
}

bool valid_email_char(char c)
{
	char* special = "<>()[]\\.,;:@\"";
	if (c == '.') return true;
	if (c <= 0x1F) return false;
	if (c == 0x7F) return false;
	if (c == ' ') return false;
	if (strchr(special,c)) return false;
	return true;
}

char[][] parse_string(char[] text)
{
	char[][] res;
	char* raw = toStringz(text);
	char* p;
	char* e;
	
	for(p = strchr(raw,'@'); p; p = strchr(e,'@')) {
		for(e = p+1; valid_email_char(*e); e++) {}
		if (e > raw && *(e-1) == '.') e--;
		for(; p > raw && valid_email_char(*(p-1)); p--) {}
		res ~= p[0..(e-p)]; //add .dup if required
	}
	
	return res;
}

Regan



More information about the Digitalmars-d mailing list