『壹』 怎麼用emeditor提取txt文件中指定的數據
把下列文字保存成《提取正則結果.js》 文件,在emeditor裡面宏調用。輸入網址的正則表達式就可以了。(網址的正則表達式網上一搜一堆,自己找找吧)
/**
* 根據輸入的正則表達式,提取與之匹配的字元串,並新建一個文檔顯示所有匹配的字元串,
* 顯示時每個匹配項佔一行
*
* @author Guapo
* @version v1.0
*/
function getText()
{
var _regex=prompt("本程序用來提取正則表達式匹配的字元串,請輸入正則表達式","");
if(_regex==""||_regex==null)
{
alert("輸入為空,請重新輸入");
return;
}
var regex=new RegExp(_regex,"gim");
document.selection.SelectAll();
var textInCurrentDocument=document.selection.Text;
var matchResultArray=textInCurrentDocument.match(regex);
if(matchResultArray)
{
var len=matchResultArray.length;
editor.NewFile();
for(var i=0;i<len;i++)
{
document.writeln(matchResultArray[i]);
}
}
else
{
alert("沒有找到匹配的字元串");
}
}
/**
* 拷貝正在編輯的文檔的完整路徑(含文件名和擴展名)到剪貼板
*
* @author Guapo
* @version v1.0
*/
function FullName()
{
if(document.Saved)
{
document.CopyFullName();
}
else
{
alert("請保存文件後再執行此命令");
}
}
/**
* 拷貝正在編輯的文檔的路徑(不含文件名和擴展名)到剪貼板
*
* @author Guapo
* @version v1.0
*/
function Path()
{
if(document.Saved)
{
document.CopyPath();
}
else
{
alert("請保存文件後再執行此命令");
}
}
/**
* 刪除當前正在編輯文檔的行首的所有空格(包含Tab鍵)
*
* @author Guapo
* @version v1.0
*/
function deleteLineStartBlank()
{
document.selection.Replace("^\\s*","",eeFindNext | eeReplaceAll | eeFindReplaceRegExp);
}
/**
* 刪除當前正在編輯文檔的行尾的所有空格(包含Tab鍵)
*
* @author Guapo
* @version v1.0
*/
function deleteLineEndBlank()
{
document.selection.Replace("\\s*$","",eeFindNext | eeReplaceAll | eeFindReplaceRegExp);
}
/**
* 為當前正在編輯文檔的每一行的行首添加n個空格
*
* @author Guapo
* @version v1.0
*/
function addBlank2LineStart()
{
var string_n=prompt("請問您要給行首添加幾個空格呢?","1");
if(isN(string_n))
{
var s="";
var int_n=parseInt(string_n);
for(var i=0;i<int_n;i++)
{
s+=" ";
}
s+="\\0";
document.selection.Replace("^.",s,eeFindNext | eeReplaceAll | eeFindReplaceRegExp);
document.HighlightFind=false;
return;
}
else
{
alert("您輸入的不正確,請重新輸入");
}
}
/**
* 為當前正在編輯文檔的每一行的行尾添加n個空格
*
* @author Guapo
* @version v1.0
*/
function addBlank2LineEnd()
{
var string_n=prompt("請問您要給行尾添加幾個空格呢?","1");
if(isN(string_n))
{
var s="";
s+="\\0";
var int_n=parseInt(string_n);
for(var i=0;i<int_n;i++)
{
s+=" ";
}
document.selection.Replace(".$",s,eeFindNext | eeReplaceAll | eeFindReplaceRegExp);
document.HighlightFind=false;
return;
}
else
{
alert("您輸入的不正確,請重新輸入");
return;
}
}
/**
* 將當前正在編輯文檔中的所有>=兩個的相連空格替換成一個,只支持英文半形空格
*
* @author Guapo
* @version v1.0
*/
function replace2MoreBlank()
{
document.selection.Replace(" {2,}"," ",eeFindNext | eeReplaceAll | eeFindReplaceRegExp);
}
/**
* 將當前正在編輯文檔中的所有>=兩個的相連空行替換成一個
*
* @author Guapo
* @version v1.0
*/
function replace2MoreNewLine()
{
return;
}
/**
* 為當前正在編輯文檔中的所有行的行首添加特定的字元串
*
* @author Guapo
* @version v1.0
*/
function addString2LineStart()
{
var s=prompt("請問您要在行首添加什麼字元串呢?","在這里輸入要添加的字元串");
if(s==""||s==null)
{
return;
}
var t="\\0";
document.selection.Replace("(^.)|(^\\n)",s+t,eeFindNext | eeReplaceAll | eeFindReplaceRegExp);
document.HighlightFind=false;
}
/**
* 為當前正在編輯文檔中的所有行的行尾添加特定的字元串
*
* @author Guapo
* @version v1.0
*/
function addString2LineEnd()
{
var s=prompt("請問您要在行尾添加什麼字元串呢?","在這里輸入要添加的字元串");
if(s==""||s==null)
{
return;
}
var t="\\0";
document.selection.Replace("(.$)|(^\\n$)",t+s,eeFindNext | eeReplaceAll | eeFindReplaceRegExp);
document.HighlightFind=false;
}
function isN(s)
{
if(s==null||s=="")
{
return false;
}
var array=s.match(/\d+/);
if(array)
{
if(s==array[0])
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
mainMenu=CreatePopupMenu();
mainMenu.Add("拷貝路徑(不含文件名)",1);
mainMenu.Add("拷貝完整路徑(含文件名)",2);
mainMenu.Add( "", 0, eeMenuSeparator );
mainMenu.Add("利用正則表達式提取字元串",3);
mainMenu.Add( "", 0, eeMenuSeparator );
mainMenu.Add("刪除行首所有空格",4);
mainMenu.Add("刪除行尾所有空格",5);
mainMenu.Add("行首添加n個空格",6);
mainMenu.Add("行尾添加n個空格",7);
mainMenu.Add( "", 0, eeMenuSeparator );
mainMenu.Add("將>=兩個的相連空格替換成一個",8);
mainMenu.Add("將>=兩個的相連空行替換成一行",9);
mainMenu.Add( "", 0, eeMenuSeparator );
mainMenu.Add("在行首添加特定的字元串",10);
mainMenu.Add("在行尾添加特定的字元串",11);
mainMenu.Add( "", 0, eeMenuSeparator );
mainMenu.Add("關於",100);
var witchItem=mainMenu.Track();
switch(witchItem)
{
case 1:
Path();
break;
case 2:
FullName()
break;
case 3:
getText();
break;
case 4:
deleteLineStartBlank();
break;
case 5:
deleteLineEndBlank();
break;
case 6:
addBlank2LineStart();
break;
case 7:
addBlank2LineEnd();
break;
case 8:
replace2MoreBlank();
break;
case 9:
replace2MoreNewLine();
break;
case 10:
addString2LineStart();
break;
case 11:
addString2LineEnd();
break;
case 100:
alert("Author:Guapo\nEmail:[email protected]");
break;
default:
break;
}
『貳』 正則表達式從文件路徑中提取文件名
String input = "D:\\save\\任意文件名.pdf";
String regex = ".*\\\\([^\\.]+)\\..*";
input.replaceAll(regex, "$1");