簡體   English   中英

正則表達式-精確匹配10位數字,其中至少有一個符號或空格

[英]regex - match exactly 10 digits with atleast one symbol or spaces between them

我正在嘗試在oracle sql中編寫查詢,以獲取具有無效10位數字的行,即在它們之間具有其他符號。

例如:

(111) 111-1111       #10 digit number with some symbols and spaces in between
111-111-1111         
(111)111-1111
111)111-1111
(111) 11    1-1111

即,它應該精確匹配不連續的10位數字,因為其中有一些符號。

因此,它不應與以下示例匹配:

111          #consecutive 3 digit number
11   1       #3 digit number with spaces
11-1         #3 digit number with symbol in between
1111111111   #consective 10 digit number

我正在使用REGEXP_LIKE,像這樣

select * from table where REGEXP_LIKE(column, ?)

任何幫助深表感謝。 謝謝。

您可以結合使用正則表達式和length 后者排除不帶其他字符的純10位數字:

regexp_like(col, '^[ .()-]*(\d[ .()-]*){10}$') and length(col) > 10

[.()-]類中,您將列出所有允許用作數字符號的字符。 請注意, -必須是該列表中的最后一個,否則必須進行轉義。

如果您允許10位中的任何非數字出現,則可以使用\\D

regexp_like(col, '^\D*(\d\D*){10}$') and length(col) > 10

因此:該字符串的長度應大於10,並且數字的總位數必須恰好為10。這可以在不使用正則表達式的情況下完成(這應使其更快):

... where length(str) > 10 and
          length(str) = 10 + length(translate(str, 'z0123456789', 'z'))

translate會將字母z轉換為自身,而將所有其他字符(數字)轉換為空。 必須包含z很煩人,但不可避免。 如果其任何參數為NULL, translate將返回NULL。 第二個條件是輸入str的長度比除去所有數字的字符串的長度正好10倍-因此正好有10個數字。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM