如何在正则表达式中匹配 \n 但从以下文本中跳过 n?

Posted

技术标签:

【中文标题】如何在正则表达式中匹配 \\n 但从以下文本中跳过 n?【英文标题】:How to match \n in regex but skip n from following text?如何在正则表达式中匹配 \n 但从以下文本中跳过 n? 【发布时间】:2021-03-20 22:45:49 【问题描述】:

我正在尝试从提到的字符串中提取数字数据,但是,当使用给定的模式时,我也错过了以下字符串。

模式:[^\\n)]+

409416,-15.84361,-22.66174,-15.777729,-11.565274,0.184927,2.184308,-2.918847,-1.438143,-1.832789,-2.392894,-2.936923,-1.699626,-1.699626,-0.559298,-0.559298,-0.559298,-0.559298,-0.559298\n0.268223,0.088596,-0.953149,-1.344175,0.197503,4.143355,3.463934,0.289587,-0.063034,0.35563,2.322007,-13.589606,-11.883781,17.186039,-7.376517,10.132304,-4.420093,0.77321,5.358715,3.092631,0.457418,1.67359,4.545597,1.758356,1.758356,0.544843,0.544843,0.544843,0.544843,0.544843\n-4.421537,-2.864239,-3.992804,-2.769629,-0.345838,1.462282,-0.733731,-1.554252,0.376582,5.262342,7.720245,-14.295092,-14.852295,-16.991022,15.644931,14.116446,-4.67732,-6.69726,-0.406152,1.403272,-1.297639,-2.341637,-1.378868,-2.402558,-2.402558,-3.345482,-3.345482,-3.345482,-3.345482,-3.345482\n0.303624,-1.55541,-1.163894,-0.002663,1.203844,0.47408,-1.725865,-1.635311,-0.809665,1.496815,0.127842,2.615432,1.528776,-34.86355,4.610298,1.973559,-2.828502,1.598024,1.195854,0.623229,-1.526112,-0.921527,-0.346238,-0.905547,-0.905547,0.348902,0.348902,0.348902,0.348902,0.348902\n0.03196,-1.725865,-1.523449,-1.086656,-0.183773,0.516694,0.561972,0.292971,-0.183773,-0.002663,-2.048133,-13.026555,-17.415792,29.832436,3.382483,2.988304,-1.811093,0.114525,0.386189,-0.628556,-1.704558,-1.853707,-1.222488,-1.182537,-1.182537,-0.255684,-0.255684,-0.255684,-0.255684,-0.255684\n0.287644,-1.054696,-1.134597,-0.761725,0.109198,0.242367,-0.415486,-0.191763,-0.514031,0.138495,0.596595,4.54904,-4.29602,5.593082,7.870266,2.460956,1.787123,0.70313,-0.258347,0.103872,-0.26101,-0.058594,0.189099,0.713784,0.713784,-0.114525,-0.114525,-0.114525,-0.114525,-0.114525',Badminton_Smash

必填:不带\n的字符串。

链接:https://regex101.com/r/sMtFzd/1

【问题讨论】:

请将您的示例减少到可以证明问题的最小长度。 【参考方案1】:

使用拆分

\\n|\)

见proof。

C++ 支持拆分,见Split a string using C++11。

使用

#include <iostream>
#include <regex>
using namespace std;

std::vector<std::string> split(const string& input, const string& regex) 
    // passing -1 as the submatch index parameter performs splitting
    std::regex re(regex);
    std::sregex_token_iterator
        firstinput.begin(), input.end(), re, -1,
        last;
    return first, last;

    
int main() 
    std::string input("409416,-15.84361,-22.66174,-15.777729,-11.565274,0.184927,2.184308,-2.918847,-1.438143,-1.832789,-2.392894,-2.936923,-1.699626,-1.699626,-0.559298,-0.559298,-0.559298,-0.559298,-0.559298\n0.268223,0.088596,-0.953149,-1.344175,0.197503,4.143355,3.463934,0.289587,-0.063034,0.35563,2.322007,-13.589606,-11.883781,17.186039,-7.376517,10.132304,-4.420093,0.77321,5.358715,3.092631,0.457418,1.67359,4.545597,1.758356,1.758356,0.544843,0.544843,0.544843,0.544843,0.544843\n-4.421537,-2.864239,-3.992804,-2.769629,-0.345838,1.462282,-0.733731,-1.554252,0.376582,5.262342,7.720245,-14.295092,-14.852295,-16.991022,15.644931,14.116446,-4.67732,-6.69726,-0.406152,1.403272,-1.297639,-2.341637,-1.378868,-2.402558,-2.402558,-3.345482,-3.345482,-3.345482,-3.345482,-3.345482\n0.303624,-1.55541,-1.163894,-0.002663,1.203844,0.47408,-1.725865,-1.635311,-0.809665,1.496815,0.127842,2.615432,1.528776,-34.86355,4.610298,1.973559,-2.828502,1.598024,1.195854,0.623229,-1.526112,-0.921527,-0.346238,-0.905547,-0.905547,0.348902,0.348902,0.348902,0.348902,0.348902\n0.03196,-1.725865,-1.523449,-1.086656,-0.183773,0.516694,0.561972,0.292971,-0.183773,-0.002663,-2.048133,-13.026555,-17.415792,29.832436,3.382483,2.988304,-1.811093,0.114525,0.386189,-0.628556,-1.704558,-1.853707,-1.222488,-1.182537,-1.182537,-0.255684,-0.255684,-0.255684,-0.255684,-0.255684\n0.287644,-1.054696,-1.134597,-0.761725,0.109198,0.242367,-0.415486,-0.191763,-0.514031,0.138495,0.596595,4.54904,-4.29602,5.593082,7.870266,2.460956,1.787123,0.70313,-0.258347,0.103872,-0.26101,-0.058594,0.189099,0.713784,0.713784,-0.114525,-0.114525,-0.114525,-0.114525,-0.114525',Badminton_Smash");
    std::string rgx("\\\\n|\\)");
    for (auto const c : split(input, rgx)) 
        std::cout << c << "\n";
    
    return 0;

见C++ proof。

【讨论】:

我需要字符串匹配 'without' \n,实际上,我正在使用 C++ 进行清理。正则表达式网站用于生成正确的正则表达式。 @BhaskarDhariyal 刚刚发现一个关于 C++ 正则表达式拆分代码的问题。请检查更新。

以上是关于如何在正则表达式中匹配 \n 但从以下文本中跳过 n?的主要内容,如果未能解决你的问题,请参考以下文章

使用正则表达式和 sed 时跳过前 n 行?

如何在字符串中返回第 n 个正则表达式匹配?

如何在 N-Queen 问题中跳过重复的棋盘状态?

VBA 正则表达式 如何匹配其中包含\ 且非"\n" 非"\t"的文本?

如何使用正则表达式匹配 className 内的文本? [复制]

如何在 Python 中使用正则表达式将所有内容匹配到双换行符“\n\n”?