用正則進行字串切分的乙個應用

2021-04-13 11:26:04 字數 910 閱讀 6994

想解決下面乙個問題:

比如給定下面乙個字串,你好:,這是您第次登入。

我想把<>裡的文字替換成別的,比如替換成從資料庫中讀取出來的資料。

如果還需要注意這樣乙個問題:《和》如果是真實的字元怎麼辦?

我是這樣想的,用字元/《代表真正的<.也就是/,<,>均具有特殊含義。必須用//,/<,/>來代替。

我是這樣想的,用正規表示式區分割,先把/轉化了,再轉化《和》.

雖然實現了,但總覺得寫的不好。剛學正則,不大會用。

高人指點下。

感覺這段程式很實用。希望能完善。大家幫忙想想啊。

具體的程式如下:

string regex = "/<";

string st r= "/函式一://函式2: 測試 ";

pattern p = pattern.compile(regex);

matcher m = p.matcher(str);

string s1 = "";

int startpos = 0;

int start = 0;

int end = 0;

system.out.println("length:"+str.length());

while(m.find())

if(end<=str.length())

string reg1 = "<[^<][^>]*([^/]>)";

p = pattern.compile(reg1);

m = p.matcher(s1);

startpos = 0;

start=0;

end=0;

system.out.println("length:"+s1.length());

while(m.find())

if(end

實現乙個模組module用來進行字串的逆置

實現如下 include include module license gpl module author weipeng jing module description hello world module module alias a st module static char data pan...

在乙個長字串中刪除乙個短字串

輸入兩個字串,從第一字串中刪除第二個字串中所有的字元。例如,輸入 they are students.和 aeiou 則刪除之後的第乙個字串變成 thy r stdnts.這裡注意 從乙個字串中刪除字串的時候,如果是通過刪除乙個字串就移動後面所有的字元,就非常複雜了。通過hash表記錄第二個字串 現...

字串問題 將乙個字串中的單詞進行倒置

2013 09 15 14 49 09 將乙個字串中的單詞進行倒置,標點符號也倒置,如輸入 hello,nice to meet you 輸出 you meet to nice,hello 將 中的while pcur isalphabet pcur 換成while pcur pcur 即可實現翻轉...