每天記錄學習的新知識 正規表示式限制車牌號碼

2021-10-01 02:45:19 字數 4436 閱讀 5569

車牌號的正規表示式的規則目前是:

"([京津滬渝冀豫雲遼黑湘皖魯新蘇浙贛鄂桂甘晉蒙陝吉閩貴粵青藏川寧瓊使領a-z]"

+"[a-z]"

+"(([0-9][df])|([df]([a-hj-np-z0-9])[0-9])))|"

+"([京津滬渝冀豫雲遼黑湘皖魯新蘇浙贛鄂桂甘晉蒙陝吉閩貴粵青藏川寧瓊使領a-z]"

+"[a-z]"

+"[a-hj-np-z0-9]"

+"[a-hj-np-z0-9掛學警港澳])"

;

時間是2019.11.28

這麼寫是可以判斷當前字串是否正確,但是不能限制輸入,也不能判斷車牌號是否輸入完整,所以我對正則規則進行了拆分,達到限制文字輸入的目的和得知車牌號是否輸入完整並且正確。

在呼叫的地方呼叫如下**:

@ontextchanged

(value = r.id.edittext, callback = ontextchanged.callback.text_changed)

void

onvehiclenumtextchanged

(charsequence s,

int start,

int before,

int count)

}

關鍵方法是我們的正規表示式:

拆分規則,把每個位置的規則進行拆分比較

private

static pattern common1 =

compile

("[京津滬渝冀豫雲遼黑湘皖魯新蘇浙贛鄂桂甘晉蒙陝吉閩貴粵青藏川寧瓊使領a-z]");

private

static pattern common2 =

compile

("[a-z]");

private

static pattern common3_n1_pt =

compile

("[0-9]");

private

static pattern common3_n2_pt =

compile

("[df]");

private

static pattern common3_else =

compile

("[a-hj-np-z]");

private

static pattern common4_n1_pt =

compile

("[0-9]");

private

static pattern common4_n1_pt_else =

compile

("[a-hj-np-z]");

private

static pattern common4_n2_pt =

compile

("[a-hj-np-z0-9]");

private

static pattern pattern4_pt =

compile

("[a-hj-np-z0-9]");

private

static pattern common5_n1_pt =

compile

("[0-9]");

private

static pattern common5_n1_pt_else =

compile

("[a-hj-np-z]");

private

static pattern common5_n2_pt =

compile

("[0-9]");

private

static pattern common5_n2_pt_else =

compile

("[a-hj-np-z]");

private

static pattern pattern5_pt =

compile

("[a-hj-np-z0-9]");

private

static pattern common6_n1_pt =

compile

("[0-9]");

private

static pattern common6_n1_pt_else =

compile

("[a-hj-np-z]");

private

static pattern common6_n2_pt =

compile

("[0-9]");

private

static pattern common6_n2_pt_else =

compile

("[a-hj-np-z]");

private

static pattern pattern6_pt =

compile

("[a-hj-np-z0-9]");

private

static pattern pattern7_n1 =

compile

("[0-9]");

private

static pattern pattern7_n2_pt =

compile

("[0-9]");

private

static pattern pattern7_pt =

compile

("[a-hj-np-z0-9掛學警港澳]");

private

static pattern pattern8_n1 =

compile

("[df]");

private

static pattern pattern8_n2 =

compile

("[0-9]"

);

設乙個狀態變數,儲存車牌號是否正確

/**

* 車牌號是否正確(這個正確包括輸入完整)

*/public

static

boolean iscarnumok =

false

;

邏輯方法

public

static string checkcarnumregex

(string str)}}

else

if(pattern7_pt.

matcher

(getlocationstr

(str,7)

).matches()

)}}else

if(common6_n1_pt_else.

matcher

(getlocationstr

(str,6)

).matches()

)}}}

}else

if(common5_n1_pt_else.

matcher

(getlocationstr

(str,5)

).matches()

)}}}

}}}else

if(common4_n1_pt_else.

matcher

(getlocationstr

(str,4)

).matches()

)}}}

}}}}

}else

if(common3_n2_pt.

matcher

(getlocationstr

(str,3)

).matches()

)}}else

if(pattern7_pt.

matcher

(getlocationstr

(str,7)

).matches()

)}}else

if(common6_n2_pt_else.

matcher

(getlocationstr

(str,6)

).matches()

)}}}

}else

if(common5_n2_pt_else.

matcher

(getlocationstr

(str,5)

).matches()

)}}}

}}}}

}else}}

}}}}

}}}}

}}}}

return result.

tostring()

;}

獲取string每個位置的值,(從1開始)

private

static string getlocationstr

(string string,

int location)

每天記錄學習的新知識 getParentFile

用於獲取檔案的目錄結構.一般用於操作檔案前,對檔案的目錄結構審查 file fileparent file.getparentfile 乙個簡單的小例子,用於建立檔案前,確保檔案目錄滅有問題。string strpath e a aa aaa.txt file file new file strpa...

每天記錄學習的新知識 RM 刪除命令

rm是linux命令 刪除檔案時,盡量使用絕對路徑或者進入到目標路徑下後使用刪除命令。禁止使用rm rf 檔案 少個 就是根目錄,會造成不可回退的災難 語法 rm options name.rm 選項 引數 引數為目錄或檔案 刪除當前目錄下的所有檔案及目錄,並且是直接刪除,無需逐一確認命令行為 rm...

每天記錄學習的新知識 AM 呼叫命令

適用於linux 環境 和 adb shell 下 撥打 am start a android.intent.action.call d tel 10086 開啟 www.baidu.com 啟動activity am start options am start n 包 package 名 包名....