關於String替換操作的一點筆記

2021-06-19 02:55:13 字數 1776 閱讀 4185

最近專案需要抓取學校百合的一些熱點資訊,免不了頻繁使用正則和string的一些替換操作,遇到了一些問題,值得小記一下。

下面是乙個操作的片段

pattern textareacontent = pattern.compile("(?s)(
matcher contentmatcher = textareacontent.matcher(resulthtml);

stringbuffer buff = new stringbuffer();

while(contentmatcher.find())

char nextchar = replacement.charat(cursor);

if (nextchar == '\\') else if (nextchar == '$') else

}if (gsb.length() == 0)//如果buffer裡沒有就報錯

throw new illegalargumentexception(

"named capturing group has 0 length name");

if (nextchar != '}')

throw new illegalargumentexception(

"named capturing group is missing trailing '}'");

string gname = gsb.tostring();

if (ascii.isdigit(gname.charat(0)))//組名不可能以數字開頭

throw new illegalargumentexception(

"capturing group name starts with digit character");

if (!parentpattern.namedgroups().containskey(gname))//在pattern中查詢組

throw new illegalargumentexception(

"no group with name ");

refnum = parentpattern.namedgroups().get(gname);

cursor++;

} else

int nextdigit = replacement.charat(cursor) - '0';

if ((nextdigit < 0)||(nextdigit > 9))

int newrefnum = (refnum * 10) + nextdigit;

if (groupcount() < newrefnum) else }}

if (start(refnum) != -1 && end(refnum) != -1)

}

處理的方法:matcher.quotereplacement()

if ((s.indexof('\\') == -1) && (s.indexof('$') == -1))

return s;

stringbuilder sb = new stringbuilder();

for (int i=0; i在特殊字前插入'\\'『;

另外string.replace()

public string replace(charsequence target, charsequence replacement)

是通過matcher.replaceall來實現的。

關於移位操作的一點說明

我們有下面的測試 對於正數的移位操作 7的二進位製碼為0111 當左移2位時 右邊補0 11100 十進位制28 當右移2位時 左邊補0 001 十進位制1 7的二進位製碼1111 當左移兩位時 111100 最高位為符號位 十進位制 28 當右移1位時 二進位製碼為1111 取反加一為補碼 100...

關於作業系統的一點東西

作業系統的組成 系統資源包括cpu 記憶體 輸入輸出裝置以及儲存在外存中的資訊.因此作業系統由 1 對cpu的使用進行管理的程序排程程式 2 對記憶體分配進行管理的記憶體管理程式 3 對輸入輸出裝置進行管理的裝置驅動程式 4 對外存中資訊進行管理的檔案系統 bios bios是基本輸入輸出系統 ba...

關於iBatis selectKey的一點筆記

技術前提 我們使用ibatis作為持久層方案 技術場景 假設我們有兩張表,一張主表main,一張子表sub,並且主表的主鍵是由資料庫維護的自增長的主鍵,子表中有乙個字段引用這個主鍵,那麼當我們插入主表資料後,就需要馬上返回這個自增長的主鍵。解決方案 可以在insert時通過ibatis的select...