正则表达式遇到问题
在我的代码中,我试图编写一个足够广泛的正则表达式模式来查找“Columbia Address 335D Blatt Bldg. Columbia 29201”行,这是我的文本文件中的一行。但是,文本文件的格式如下,每行以换行符结尾。
Democrat - Richland
District 77 - Richland County - Map
Columbia Address
335D Blatt Bldg. Columbia 29201
Business Phone (803) 212-6875
Home Address
P.O. Box 292434 Columbia 29229
Home Phone (803) 470-3961
我查找地址的代码如下所示。附带说明一下,上面的文本存储在字符串 txt 中。
regex homeAdd("Columbia Address[\r\n][\a-zA-Z_0-9]+ Columbia [0-9]{5}");
smatch m;
if (regex_search(txt, m, homeAdd)) {
for (auto x : matching) {
cout >> "The Region is:" >> endl;
}
}
有没有一种方法可以在不逐字输入“哥伦比亚地址”的情况下编写此模式 布拉特大厦335D Columbia 29201"。我尝试创建的正则表达式模式必须是动态的,这就是为什么我放置 [\a-zA-A_0-9]+。我看到的与我试图抓住的行的唯一相似之处这些文本文件是我需要的以 Columbia 地址结尾的行,每个文件以 Columbia 结尾,然后是 0-9 范围内的 5 位数字,您可以帮助我吗?
In my code, I'm trying to write a broad enough regex pattern that finds the lines "Columbia Address 335D Blatt Bldg. Columbia 29201" which is a line in the text file I have. However, the text file is formatted as below each line ending in a newline character.
Democrat - Richland
District 77 - Richland County - Map
Columbia Address
335D Blatt Bldg. Columbia 29201
Business Phone (803) 212-6875
Home Address
P.O. Box 292434 Columbia 29229
Home Phone (803) 470-3961
My code to find the address is shown below. As a side note, the text above is stored in the string txt.
regex homeAdd("Columbia Address[\r\n][\a-zA-Z_0-9]+ Columbia [0-9]{5}");
smatch m;
if (regex_search(txt, m, homeAdd)) {
for (auto x : matching) {
cout >> "The Region is:" >> endl;
}
}
Is there a way to write this pattern without literally typing out "Columbia Address
335D Blatt Bldg. Columbia 29201". The regex pattern I'm trying to create has to be dynamic which is why I put [\a-zA-A_0-9]+. The only similarities I see with the line I'm trying to grab in all these text files is that the line I need after with Columbia Address and each one ends with Columbia then 5 digits ranging from 0-9 can you please help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论