Regular Expression 正则表达式-4 (C++)

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

因为总觉得上回写的C++代码太过蹩脚了,心有不甘。毕竟C++是一个很优秀的语言,并且有着众多出色的模板库,这么简单的一个小程序被我给用成那样,真的是太惭愧了。代码绝对不应该这么臃肿。实际上我有几个概念模糊不清了,所以导致了代码的臃肿,一个是输入输出流的概念模糊了,还有一个是正则表达式应用不熟悉。于是重温了输入输出流,并且详细的阅读了正则表达式的Boost库的说明文档。新写的代码如下,明显比原来的代码优雅了许多:

#include <string>
#include <boost/regex.hpp>
#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;
using namespace boost;

string filter
( const string in )
{
    
regex expr("/"(//w+):(//w+)%(//w+)/"");
    
string fmt("$1:$2*$3,/n");
    
ostringstream ostring;
    
ostream_iterator<char> oi(ostring);

    
regex_replace( oi, in.begin(), in.end(), expr, fmt, (match_default | format_no_copy) );

    return
ostring.str();
}

int main
(int argc, const char* argv[])
{
    if(
argc < 3 )
    {
        
cout<< "Please enter 2 filenames(e.g. In.txt Out.txt)" << endl;
        return
1;
    }

    
ifstream in( argv[1] );
    
ofstream out( argv[2] );
    
ostringstream buf;

    
buf << in.rdbuf();
    
out << filter( buf.str() ) << flush;
}

[LeetCode] Regular Expression Matching 解题报告 [题目] mplement regular expression matching with support for ‘.’ and ‘*’. ‘.’ Matches any single character. ‘*’ Matches zero or more of the preceding element. The matching should cover the ent 阅读详情

相关推荐

Leetcode : Regular Expression Matching

对于leetcode上这个题目,我用了不少时间来消化。 题目大意如下: 实现两个字符串s,t的匹配,其中t字符串中的 ‘.’ 能匹配任何一个字符. ‘*’ 能充当0个或者多个前面一个字符. 匹配结果要覆盖整个字符串 几个例子: isMatch(“aa”,”a”) → false isMatch(“aa”,”aa”) → true isMatch(“aaa”,”aa”) → f

等价交换 668

C++11 06-正则表达式 regular expression

命名捕获组在正则表达式中通过?语法定义。其中name是你为捕获组指定的名称。

wesigj的博客 1963

regular_expression

本文将介绍 C++ 正则表达式的常见用法

qq_38413468的博客 1785

Regular Expression:C++正则表达式库(RE库<regex>)

正则表达式(Regular Expression)是一种描述字符序列的方法,是处理字符序列的一种强大的计算工具!(字符串的匹配、查找、替换) 1.ECMAScript :ECMA-262规范: ECMAScript是一种由Ecma国际(前身为欧洲计算机制造商协会,英文名称是European Computer Manufacturers Association)通过ECMA-262标准化的脚本程...

m0_37357063的博客 1056

Regular Expression 正则表达式-3 (C++)

最后用C++实现了一把,因为STL中尚未包含Regular Expression,因此我使用了Boost中的Regex++。不过因为不是很熟悉,所以代码很蹩脚,将就看了。呵呵。#include #include #include #include using namespace std; void readFile( const char* filename, string

DanceFire的专栏 3176

【LeetCode】10. Regular Expression Matching(C++

地址:https://leetcode.com/problems/regular-expression-matching/ 题目: Given an input string (s) and a pattern (p), implement regular expression matching with support for '.' and '*'. ‘.’ Matches any sing...

Ethan95的博客 422

正则表达式Regular Expression)学习网址分享

正则表达式Regular expressions,也叫REs、 regexs 或regex patterns),是一种文本模式,包括普通字符(例如,a 到z 之间的字母)和特殊字符(称为"元字符"),可以用来描述和匹配字符串的特定模式。我们在最开始学习的阶段会去搜索很多的资料并且需要花费很多时间在资料整合上,我写这篇笔记的目的在于能够给大家分享我使用最频繁的几个学习正则表达式的网址,这样可以方便大家更加快速的学习和上手应用。

GOLOJO的博客 924

LeetCode—10.正则表达式匹配(Regular Expression Matching)——分析及代码(C++

LeetCode—10.正则表达式匹配[Regular Expression Matching]——分析及代码[C++]一、题目二、分析及代码1. 从后向前递归(1)思路(2)代码(3)结果2. 动态规划(1)思路(2)代码(3)结果三、其他1.长度为变量的数组 一、题目 给你一个字符串 s 和一个字符规律 p,请你来实现一个支持 ‘.’ 和 ‘*’ 的正则表达式匹配。 '.' 匹配任意单个字符 ...

江南土豆的博客 509

regular expression matching 正则匹配

/* Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input

outlier的博客 676

LeetCode第十题:Regular Expression Matching(C++)详解

Regular Expression Matching Given an input string (s) and a pattern §, implement regular expression matching with support for ‘.’ and ‘*’. ‘.’ Matches any single character. ‘*’ Matches zero or more of...

WangHuanToBeBest 662

[LeetCode]Regular Expression Matching、Wildcard Matching

Regular Expression Matching: Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matchin

a83610312的专栏 5748

Leetcode 10 Regular Expression Matching

正则表达式匹配,题意理解要注意.*可以理解为任意字符串,例如: isMatch("ab", ".*") → true “.”和普通字符的匹配非常容易,关键在于“*”, 由于不知道它可能代表多少个字符,而前面的决定会影响后面,所以自然而然想到DFS。 DFS暴搜的时候需要注意边界条件和终止搜索的条件,详见代码。

无名山丘,崛起成峰 1672

LeetCode10. Regular Expression Matching (C++/Python)

Given an input string (s) and a pattern (p), implement regular expression matching with support for'.'and'*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. ...

qq_41562704的博客 356

Regular Expression Matching

题目大意给定两个字符串a,b,要求判断a是否能匹配b,其中b中存在两类特殊字符’.’和’*’。 ”’ 匹配任何单个字符。 ‘*’匹配零个或多个前面的元素。 关于’*’: “ab” maches “a*b”; “aaaaaa” maches “a*”; “cab” maches “c*a*b”;解题思路从题目给的标签来看可以用回溯和动态规划来解题。解体的关键是如何处理字符’‘,对于字符’.

cdcupt的博客 335

LeetCode —— Regular Expression Matching

链接:http://leetcode.com/onlinejudge#question_10 原题: Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding

niaokedaoren的专栏 1191

Regular Expression Matching -- LeetCode

原题链接: http://oj.leetcode.com/problems/regular-expression-matching/  这个题目比较常见,但是难度还是比较大的。我们先来看看brute force怎么解决。基本思路就是先看字符串s和p的从i和j开始的子串是否匹配,用递归的方法直到串的最后,最后回溯回来得到结果。假设现在走到s的i位置,p的j位置,情况分为下列两种:  (1)p[j

Code Ganker 3万+
上一篇: Regular Expression 正则表达式-3 (C++)
下一篇: 利用正则表达式将html网页数据变成Web Service
DanceFire
博客等级 码龄25年 91粉丝 42原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值