check string consistancy using regex

Hello!

I need some help in code.

What I am trying to do is make filter that checks and removes invalid characters from a string (text). Rule is specified by regular expressions like :
"a-zA-Z" or "0-9_a-zA-Z\xC0-\xD6\xD8-\xF6\xF8-\xFF"

I can't find any reference how to do that, could somebody help with this?
www.boost.org

look up boost::regex

Hello t0t0

If you are on a unix/linux system, you can use the regex library, but really, your rule is simple enough that you don't need reqular expressions. It can be coded quite compactly and run faster without the reqular expressions. Of couse, if reqular expressions are part of the solution criteria then you have no choice. Here is a simple algorithm to filter the string in place with only part of your filter rule.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void filter(char* text) 
{
	char* pSrc = text;
	char* pDest = text;
	while(*pSrc != 0)  
	{
		char c = *pSrc;
		if (c >= 'a' && c <= 'z' || 
			c >='A' && c <= 'Z') 
		{
			*pDest++ = c; /* optionally you can say if (pDest != pSrc) *pDest++ = c; */
		}
		++pSrc;
	}
	*pDest = 0;
}

int main(void)
{
	char text[] = "asd!@#qwe";
	filter(text);
	return 0;
}


Last edited on
Well, I agree that boost::regex will be slower, but on the other hand your 16-line functions becomes 1-2 lines of code total using boost::regex.

For that matter, the first regular expression can be implemented in one line without boost::regex:

 
std::find_if( text, text + strlen( text ), boost::bind( isalpha, _1 ) ) != text + strlen( text );


The second one can be implemented in the same way, replacing the bind with this one:

 
boost::bind( string::find, chars_to_find, _1, 0 )


where chars_to_find is a std::string containing all of the characters to search for.
Last edited on
Topic archived. No new replies allowed.