導航:首頁 > 凈水問答 > 正則表達式過濾中文字元

正則表達式過濾中文字元

發布時間:2021-03-26 03:24:25

① 如何利用正則表達式在「指定中文字元范圍內」查找並替換字元

查找:([\u4e00-\u9fa5])\s*[,,]\s*([\u4e00-\u9fa5])
替換:$1 $2

② ORACLE中怎樣用正則表達式過濾中文字元

從表裡提取漢字, 需要考慮字元集, 不同的字元集漢字的編碼有所不同
這里以GB2312為例, 寫一函數准確地從表裡提取簡體漢字.

假設資料庫字元集編碼是GB2312, 環境變數(注冊表或其它)的字元集也是GB2312編碼
並且保存到表裡的漢字也都是GB2312編碼的

那麼也就是漢字是雙位元組的,且簡體漢字的編碼范圍是
B0A1 - F7FE
換算成10進制就是
B0 A1 F7 FE
176,161 - 247,254

我們先看一下asciistr函數的定義
Non-ASCII characters are converted to the form \xxxx, where xxxx represents a UTF-16 code unit.
但是這並不表示以 "\" 開始的字元就是漢字了

舉例如下
SQL> select * from test;

NAME
--------------------
,啊OO10哈
你好aa
大家好aa/
☆大海123
★ABC

這里第5條記錄有一個實心的五角星
然後用asciistr函數轉換一下試試
SQL> select name,asciistr(name) from test;

NAME ASCIISTR(NAME)
-------------------- ----------------------
,啊OO10哈 ,\554AOO10\54C8
你好aa \4F60\597Daa
大家好aa/ \5927\5BB6\597Daa/
☆大海123 \2606\5927\6D77123
★ABC \2605ABC

我們看到最後一條記錄的實心五角星也是 "\"開頭的
此時我們就不能用asciistr(欄位)是否存在 "\" 來判斷是否含有漢字了.

我的函數如下,基本思路是判斷字元的編碼是否在GB2312規定的漢字編碼范圍之內
[PHP]
create or replace function get_chinese(p_name in varchar2) return varchar2
as
v_code varchar2(30000) := '';
v_chinese varchar2(4000) := '';
v_comma pls_integer;
v_code_q pls_integer;
v_code_w pls_integer;
begin
if p_name is not null then
select replace(substrb(mp(p_name,1010),instrb(mp(p_name,1010),'ZHS16GBK:')),'ZHS16GBK: ','') into v_code from al where rownum=1;
for i in 1..length(p_name) loop
if lengthb(substr(p_name,i,1))=2 then
v_comma := instrb(v_code,',');
v_code_q := to_number(substrb(v_code,1,v_comma-1));
v_code_w := to_number(substrb(v_code,v_comma+1,abs(instrb(v_code,',',1,2)-v_comma-1)));
if v_code_q>=176 and v_code_q<=247 and v_code_w>=161 and v_code_w<=254 then
v_chinese := v_chinese||substr(p_name,i,1);
end if;
v_code := ltrim(v_code,'1234567890');
v_code := ltrim(v_code,',');
end if;
v_code := ltrim(v_code,'1234567890');
v_code := ltrim(v_code,',');
end loop;
return v_chinese;
else
return '';
end if;
end;
/
.
[/PHP]

好,現在來執行一些語句
SQL> select * from test;

NAME
--------------------
,啊OO10哈
你好aa
大家好aa/
☆大海123
★ABC

5 rows selected.

1. 列出有漢字的記錄
SQL> select name from test where length(get_chinese(name))>0;

NAME
--------------------
,啊OO10哈
你好aa
大家好aa/
☆大海123

4 rows selected.

2. 列出有漢字的記錄,並且只列出漢字

SQL> select get_chinese(name) from test where length(get_chinese(name))>0;

GET_CHINESE(NAME)
---------------------------------------------------------------------------
啊哈
你好
大家好
大海

4 rows selected.

需要說明的是GB2312共有6763個漢字,即72*94-5=6763
我這里是計算72*94,沒有減去那5個,那五個是空的。等查到了再減去
============

改寫這個函數,可以提取非漢字或者漢字
該函數有兩個參數,第一個表示要提取的字元串,第二個是1,表示提取漢字,是非1,表示提取非漢字

[PHP]
create or replace function get_chinese
(
p_name in varchar2,
p_chinese in varchar2
) return varchar2
as
v_code varchar2(30000) := '';
v_chinese varchar2(4000) := '';
v_non_chinese varchar2(4000) := '';
v_comma pls_integer;
v_code_q pls_integer;
v_code_w pls_integer;
begin
if p_name is not null then
select replace(substrb(mp(p_name,1010),instrb(mp(p_name,1010),'ZHS16GBK:')),'ZHS16GBK: ','') into v_code from al where rownum=1;
for i in 1..length(p_name) loop
if lengthb(substr(p_name,i,1))=2 then
v_comma := instrb(v_code,',');
v_code_q := to_number(substrb(v_code,1,v_comma-1));
v_code_w := to_number(substrb(v_code,v_comma+1,abs(instrb(v_code,',',1,2)-v_comma-1)));
if v_code_q>=176 and v_code_q<=247 and v_code_w>=161 and v_code_w<=254 then
v_chinese := v_chinese||substr(p_name,i,1);
else
v_non_chinese := v_non_chinese||substr(p_name,i,1);
end if;
v_code := ltrim(v_code,'1234567890');
v_code := ltrim(v_code,',');
else
v_non_chinese := v_non_chinese||substr(p_name,i,1);
end if;
v_code := ltrim(v_code,'1234567890');
v_code := ltrim(v_code,',');
end loop;
if p_chinese = '1' then
return v_chinese;
else
return v_non_chinese;
end if;
else
return '';
end if;
end;
/

.
[/PHP]
SQL> select * from a;

NAME
--------------------
我們啊、
他(艾呀)是★們
他的\啊@

SQL> select get_chinese(name,1) from a;

GET_CHINESE(NAME,1)
-----------------------------------------
我們啊
他艾呀是們
他的啊

SQL> select get_chinese(name,0) from a;

GET_CHINESE(NAME,0)
-----------------------------------------

()★
\@

SQL>

③ 如何用正則表達式匹配漢字

var str = '你好地世界世界';
假如想用正則表達式的方式找出str中'地'的下標;
var re=/\地/;
console.log(str.search(re));
控制台列印出 2.

(這就是其中一個方法,直接在想匹配的漢字前面加\),比如 /\地/

④ Python 正則表達式 支持批量語料過濾中文字元之間的空格

|^

#encoding:UTF-8
importre
importsys
reload(sys)
sys.setdefaultencoding('utf-8')

source="你好啊hellohi"
usample=unicode(source,'utf8')
xx=u"((?<=[u4e00-u9fa5])s+(?=[u4e00-u9fa5])|回^答s+|s+$)"
temp=re.sub(xx,'',usample);
printtemp;

⑤ 如何用正則表達式去除字元串的中文

<script>
/***去除中文函數回****/
/***BY TONYLINZHEN****/
function CutChr()
{
var x=document.getElementById("text");
var str=x.value;
while(/[\u4E00-\u9FA5]+/.test(str)){
str=str.replace(/[\u4E00-\u9FA5]+/,"");
x.value=str;
}
}

</script>

<body>
<textarea cols="30" rows="30" id="text"></textarea>
<input type="button" value="提交答" onclick="CutChr()"/>
</body>

⑥ 正則表達式如何只匹配一個中文字元

php中匹配一個或多個中文字元(包含簡體和繁體中文字元)的正則表達式如下:/[[b]\x{4e00}-\x{9fa5}]+/注意:php中正則表達式的16進制是以
\x
開頭的。

⑦ 如何讓正則表達式只匹配一次中文字元

可以參考下下面的代碼,希望能幫到你:
String s = new String("大大大大大大");
Pattern pattern = Pattern.compile("^!\\p{ACSII}");//只匹配一個中文字元
Matcher matcher = pattern.matcher();
while(matcher.find()){
System.out.println(matcher.group());
}

⑧ 正則表達式匹配指定中文字元串


publicvoidShowStructure()
{
//要匹配的字元串
stringtext="早上好aaa您好bbb大家好ddd……";
//正則表達式
stringpattern=@"[u4e00-u9fff]+";
Regexr=newRegex(pattern);
//使用正則表達式匹配字元串,僅返回一次匹配結果
Matchm=r.Match(text);
while(m.Success)
{

//顯示匹配開始處的索引值和匹配到的值
System.Console.WriteLine("Match=["+m+"]");
CaptureCollectioncc=m.Captures;
foreach(Capturecincc)
{
Console.WriteLine(" Capture=["+c+"]");
}
for(inti=0;i<m.Groups.Count;i++)
{
Groupgroup=m.Groups[i];
System.Console.WriteLine(" Groups[{0}]=[{1}]",i,group);
for(intj=0;j<group.Captures.Count;j++)
{
Capturecapture=group.Captures[j];
Console.WriteLine(" Captures[{0}]=[{1}]",j,capture);
}
}
//進行下一次匹配.
m=m.NextMatch();
}
}

⑨ 正則表達式能過濾中文特殊字元嗎

String s1="我是復正確制測試數據aasdf2342343ASFASDF"; String s2="我是錯誤測試數據@#!@#"; String reg = "[^0-9a-zA-Z\u4e00-\u9fa5]+"; System.out.println(s1.replaceAll(reg,"")); System.out.println(s2.replaceAll(reg,""));

⑩ java字元串裡面如何用正則表達式去掉漢字

public static void main(String[] args) {

// TODO Auto-generated method stub

String str = "123abc你好efc";

String reg = "[u4e00-u9fa5]";

Pattern pat = Pattern.compile(reg);

Matcher mat=pat.matcher(str);

String repickStr = mat.replaceAll("");

System.out.println("去中文後:"+repickStr);

}

閱讀全文

與正則表達式過濾中文字元相關的資料

熱點內容
哪款蒸餾水敷臉比較好 瀏覽:879
印染廢水調節ph怎麼弄 瀏覽:916
奇駿換汽油濾芯多少公里換 瀏覽:491
生活污水主要含有什麼成份 瀏覽:557
法蘭妮凈水器怎麼樣 瀏覽:211
洗菜盆兒凈水器溢水口不用怎麼辦 瀏覽:803
淋浴頭被水垢堵 瀏覽:415
污水站排風每小時次數 瀏覽:450
河南污水處理行業研究 瀏覽:621
污水泵發熱了怎麼處理 瀏覽:855
帥鈴皮卡空調濾芯在哪裡 瀏覽:943
污水處理廠用地搬遷後住宅 瀏覽:842
汽車蒸餾水位置在哪 瀏覽:644
小米凈化器2怎麼解鎖 瀏覽:40
凈水器雙膜和ro膜 瀏覽:914
蒸餾東莨菪鹼 瀏覽:607
龍派凈水器多少錢呀 瀏覽:115
離子交換法處理方法 瀏覽:31
怎麼測試凈水機的凈水效果 瀏覽:489
寧波混凝土樹脂磨片專賣店 瀏覽:281