java字符串正则表达式头不包含

时间:2015-06-29 14:27:25

标签: regex linux

我有一个字符串:

[root@slave1 ~]# test -d /root/workspace/Servers || echo not found
[root@slave1 ~]# test -d /root/workspace/Servers || echo not found
not found
[root@slave1 ~]# test -d /root/workspace/Servers || echo not found
[root@slave1 ~]# test -d Desktp || echo not found
not found

我希望首先找不到not found的{​​{1}}这个词

如您所见,在此字符串中,将有2个匹配

我该怎么办?

2 个答案:

答案 0 :(得分:1)

使用负面的背后隐藏。

"(?<!\\becho )not found(?=\\s|$)"

你可以在grep中使用相同的正则表达式。

grep -oP '(?<!\becho )not found(?=\s|$)' file

(?<!\becho ) - 不要期待echo<space>

not found(?=\s|$) - 但匹配所有not found字符串,后面必须跟一个空格或行尾。

答案 1 :(得分:0)

您可以将#include <stdafx.h> #include <windows.h> #include <iostream> #include <fstream> #include <codecvt> // for wstring_convert #include <locale> // for codecvt_byname using namespace std; void BailOut(char *msg) { fprintf(stderr, "Exiting: %s\n", msg); exit(1); } int main(int argc, char* argv[]) { std::string codepage = ".1252"; if (argc > 1) { std::string cpnum = argv[1]; codepage = "."+cpnum; } HANDLE clip; string clip_text = ""; // exit if clipboard not available if (!OpenClipboard(NULL)) BailOut("Can't open clipboard"); clip = GetClipboardData(CF_TEXT); clip_text = (char*)clip; CloseClipboard(); // create conversion routines typedef std::codecvt_byname<wchar_t,char,std::mbstate_t> codecvt; std::wstring_convert<codecvt> cp1252(new codecvt(".1252")); std::wstring_convert<codecvt> outpage(new codecvt(codepage)); std::string OutFile = "#clip.txt"; // output file name ofstream OutStream; // open an output stream OutStream.open(OutFile, ios::out | ios::trunc); // make sure file is successfully opened if(!OutStream) { cout << "Error opening file " << OutFile << " for writing.\n"; return 1; } // convert to DOS/Win codepage number in "outpage" OutStream << outpage.to_bytes(cp1252.from_bytes(clip_text)).c_str(); OutStream << endl; OutStream.close(); // close output stream return 0; } 与启动锚点一起使用:

grep