匹配std :: string中的字符 [英] Matching chars in a std::string

查看:154
本文介绍了匹配std :: string中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个函数来指定一个匹配模式,包括使用

通配符,如下所示
来查找std :: string中的字符。


匹配模式可以包含通配符*和?,

其中" *"匹配零个或多个连续出现的任何

字符和?匹配任何字符的单个匹配项。


boost或其他库是否具有此功能?如果提升确实

有这个,我是否需要包含一个完整的

提升库或者我想要的位。只需使用库中的单个

实用程序功能即可获得多少额外代码大小?


谢谢

Hi, I need a function to specify a match pattern including using
wildcard characters as below
to find chars in a std::string.

The match pattern can contain the wildcard characters "*" and "?",
where "*" matches zero or more consecutive occurrences of any
character and "?" matches a single occurrence of any character.

Does boost or some other library have this capability? If boost does
have this, do i need to include an entire
boost library or just the bit i want. How much extra code size would
result from just using a single
utility function from the library?

Thanks

推荐答案

tech写道:
tech wrote:

我需要一个函数来指定匹配模式,包括使用

通配符如下所示

在std :: string中查找字符。
Hi, I need a function to specify a match pattern including using
wildcard characters as below
to find chars in a std::string.



使用正则表达式库。

Use a Regular expression library.


匹配模式可以包含通配符* ;和?,

其中" *"匹配零个或多个连续出现的任何

字符和?匹配任何字符的单个匹配项。
The match pattern can contain the wildcard characters "*" and "?",
where "*" matches zero or more consecutive occurrences of any
character and "?" matches a single occurrence of any character.



示例:

使用命名空间提升;

...

regex reg(" ^ \\\\ *。*?(\\\\ +)[^ \\\\
\\ r] * \ d?[\\\\\ ] +",regex :: mod_x);

Example:
using namespace boost;
...
regex reg("^ \\s* .*? (\\d+) [^\\n\\r]* \d? [\\n\\r]+", regex::mod_x);


boost或其他库是否具备此功能?
Does boost or some other library have this capability?



是的,它被称为boost_regex
http://www.boost.org/doc/libs/1_35_0...tml/index.html


如果boost确实有这个,我是否需要包含一个完整的

提升库或者我想要的位。只需使用库中的单个

效用函数即可获得多少额外代码大小?#
If boost does have this, do i need to include an entire
boost library or just the bit i want. How much extra code size would
result from just using a single
utility function from the library?#



在我的(Linux-)系统上,共享库的大小


/usr/lib/libboost_regex.so.1.34.1


是768320字节。


问候


M.

On my (Linux-)System, the size of the shared library

/usr/lib/libboost_regex.so.1.34.1

is 768320 bytes.

Regards

M.


6月23日,12:41 pm,Mirco Wahab< wa ... @ chemie.uni-halle.dewrote:
On Jun 23, 12:41 pm, Mirco Wahab <wa...@chemie.uni-halle.dewrote:

tech写道:
tech wrote:

我需要一个函数来指定匹配模式,包括

,使用下面的通配符来查找

std :: string中的字符。
Hi, I need a function to specify a match pattern including
using wildcard characters as below to find chars in a
std::string.


使用正则表达式库。
Use a Regular expression library.



是的,但是......

Yes, but...


匹配模式可以包含通配符*和?,

其中" *"匹配零个或多个连续出现的任何

字符和?匹配任何字符的单个匹配项。
The match pattern can contain the wildcard characters "*" and "?",
where "*" matches zero or more consecutive occurrences of any
character and "?" matches a single occurrence of any character.


示例:

使用名称空间提升;

...

正则表达式注册(" ^ \\\\ *。*?(\\\\ +)[^ \\ n\\ r] * \ d?[\ \ n \\\] +",regex :: mod_x);
Example:
using namespace boost;
...
regex reg("^ \\s* .*? (\\d+) [^\\n\\r]* \d? [\\n\\r]+", regex::mod_x);



这是一个笑话,对吧。您需要使用代码将匹配模式

转换为正则表达式;你必须将*转换为类似

" [^ /] *",例如(在Unix下 - 在Windows下,相当于

映射将是[^ / \\] *---并且在Unix下,至少,如果它是文件名中的第一件事,你还必须排除)。并且

你必须将正则表达式元字符转义为

well。


它仍然更容易使用一个正则表达式类,而不是手写,但你确实需要一些额外的代码来生成初始模式中正则表达式的




-

James Kanze(GABI软件)电子邮件:ja ********* @ gmail.com

Conseils en informatique orientéeobjet/

Beratung in objektorientierter Datenverarbeitung

9placeSémard,78210 St.-Cyr-l''coco,France,+ 33(0)1 30 23 00 34

This is a joke, right. You need code to convert a match pattern
to a regular expression; you have to convert "*'' to something like
"[^/]*", for example (under Unix---under Windows, the equivalent
mapping would be "[^/\\]*"---and under Unix, at least, if it is
the first thing in a filename, you also have to exclude .). And
you have to escape the regular expression meta-characters as
well.

It''s still easier to use a regular expression class than to do
it all by hand, but you do need some extra code to generate
the regular expression from the initial pattern.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l''école, France, +33 (0)1 30 23 00 34


James Kanze写道:
James Kanze wrote:

6月23日下午12:41,Mirco Wahab< wa。 .. @ chemie.uni-halle.dewrote:
On Jun 23, 12:41 pm, Mirco Wahab <wa...@chemie.uni-halle.dewrote:

>使用正则表达式库。
>Use a Regular expression library.



是的,但是......


Yes, but...


>示例:
使用命名空间提升;
...
正则表达式(" ^ \\\\ *。*?(\\\\ +)[^ \\\\
\\\\] * \ d ?[\\\\
\\r] +",regex :: mod_x);
>Example:
using namespace boost;
...
regex reg("^ \\s* .*? (\\d+) [^\\n\\r]* \d? [\\n\\r]+", regex::mod_x);



这是一个笑话,对吧。您需要使用代码将匹配模式

转换为正则表达式;你必须将*转换为类似

" [^ /] *",例如(在Unix下 - 在Windows下,相当于

映射将是[^ / \\] *---并且在Unix下,至少,如果它是文件名中的第一件事,你还必须排除)。并且

你必须将正则表达式元字符转义为



This is a joke, right. You need code to convert a match pattern
to a regular expression; you have to convert "*'' to something like
"[^/]*", for example (under Unix---under Windows, the equivalent
mapping would be "[^/\\]*"---and under Unix, at least, if it is
the first thing in a filename, you also have to exclude .). And
you have to escape the regular expression meta-characters as
well.



你在说什么?没有''文件名''提到

无处可去。这是用正则表达式进行纯文本处理

(如果我不完全不在路上)。

What are you talking about? There''s no ''filename'' mentioned
nowhere. It''s plain text processing with regular expressions
(if I''m not completely off the road).


它''使用正则表达式类比使用普通表达式类更容易,但是你需要一些额外的代码才能从初始模式中生成正则表达式


It''s still easier to use a regular expression class than to do
it all by hand, but you do need some extra code to generate
the regular expression from the initial pattern.



完全没有。上面的内容将是(好的我做了这个,它是一个伪表达式),一个有效的正则表达式。其他

(可能相关)的例子。查找网页中的所有链接:


int linkparser(const char * htmlname)

{

boost :: regex reg (

"(?isx-m:\

< \\\\ * A [^>] * href \\ s * = \

[\" \\\\] * \

\\\\ +://([^ \" \\\ \\ s] *)\

)"

);


string line; //读取行并每行执行一次匹配/搜索

int linecount = 0; // count lines(nice)

ifstream fin(htmlname); //打开已保存的.html文件

cout<< 试图找到中的链接<< htmlname<< endl;

while(getline(fin,line)){

++ linecount;

boost :: smatch match; //实例化匹配变量

if(boost :: regex_search(line,match,reg))

cout<< linecount<< " \t" <<匹配[1]<<结束;

}


...


上面这个表达式的哪一部分你会考虑到什么?br />
时说:

Not at all. The above would be (OK I made this up, its
a pseudo expression) a valid regular expression. Other
(maybe related) example. Find all links in a web page:

int linkparser(const char* htmlname)
{
boost::regex reg(
"(?isx-m: \
< \\s* A [^>]* href \\s* = \
[\"\\s]* \
\\w+:// ([^\"\\s]*) \
)"
);

string line; // read lines and perform one match/search per line
int linecount = 0; // count lines (nice)
ifstream fin(htmlname); // open saved .html file

cout << "trying to find links in " << htmlname << endl;
while( getline(fin, line) ) {
++linecount;
boost::smatch match; // instantiate match variable
if( boost::regex_search(line, match, reg) )
cout << linecount << "\t" << match[1] << endl;
}

...

What part of the above expression exactly would you consider
when saying:


你需要一些额外的代码来生成正则表达式
you do need some extra code to generate the regular expression



也许我们说的是不同的东西?


问候


Mirco

Maybe we speak of different things?

Regards

Mirco


这篇关于匹配std :: string中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆