RegEx Tpis

1. IsEnglishLetter
4-26 16:07:29 PM IS2120@CSDN  
1. 正则表达式,匹配(字母,数字,子母和数字)  
Only letters:  
Regex.IsMatch(input, @"^[a-zA-Z]+$");  
  
Only letters and numbers:  
Regex.IsMatch(input, @"^[a-zA-Z0-9]+$");  
  
Only letters, numbers and underscore:  
Regex.IsMatch(input, @"^[a-zA-Z0-9_]+$");  
  
bool IsEnglishLetter(char c)  
{  
    return (c>='A' && c<='Z') || (c>='a' && c<='z');  
}  
  
You can make this an extension method:  
static bool IsEnglishLetter(this char c) ...  
  
You can use Char.IsLetter(c) && c < 128 . Or just c < 128 by itself, that seems to match   
your problem the closest.  
  
But you are solving an Encoding issue by filtering chars. Do investigate what that other   
application understands exactly.  
  
It could be that you should just be writing with Encoding.GetEncoding(someCodePage)  

2. c++ windows tr1 regex (vs 2008 sp 1 以上版本)

#include <regex>
#include <string>
#include <iostream>

//z 2014-05-19 13:07:42 L.226'39138 BG57IV3@XCL T1984143998.K.F253293061 [T467,L6323,R307,V7876]
// tr1 正则表达式 例子

/*
output: 2. Egg prices
*/
void Test_Search_Match()
{
	//std::tr1::cmatch res;
	std::tr1::match_results<const char*> res;
	std::string str = "<h2>Egg prices</h2>";
	std::tr1::regex rx("<h(.)>([^<]+)");    
	std::tr1::regex_search(str.c_str(), res, rx);    
	std::cout << res[1] << ". " << res[2] << "\n";
}

/*
output : Hello planet
*/
void Test_Replace_Match()
{
    std::string str = "Hello world";    
	std::tr1::regex rx("world");    
	std::string replacement = "planet";    
	std::string str2 = std::tr1::regex_replace(str, rx, replacement);

	std::cout << str2 <<std::endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
	Test_Search_Match();
	std::cout << std::endl;
	Test_Replace_Match();

	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值