[英]Regular Expression to define a Regular Language
我需要為常規語言編寫正則表達式,其字符串始終以1開頭,偶數為0。
我已經在Java中嘗試了^ 1 +(00)+(1 | 00)*並且它接受了100,1100100,10011001等字符串,但是當0的數量是偶數時它不接受10101010。 有沒有更好的想法來定義正則表達式,拜托?
嘗試這個:
"^1+(01*01*)*$"
根據您嘗試的正則表達式,我假設只允許0和1。 如果要允許其他字符:
"^1[^0]*(0[^0]*0[^0]*)*$"
I need to write a regular expression for a regular language which its strings always start with 1 and have even number of 0s.
這個基於前瞻性的正則表達式應該適合您:
/^1(?=(([^0]*0){2})*[^0]*$)/
/^1(?=(([^0]*0){2})*[^0]*$)/.test('10'); // false
/^1(?=(([^0]*0){2})*[^0]*$)/.test('1000000'); // true
/^1(?=(([^0]*0){2})*[^0]*$)/.test('000000'); // false
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.