簡體   English   中英

正則表達式大於或小於3位數和字符

[英]Regex for greater than or less than 3 digits and characters

這可能只是通常的“哦,不要看另一個人要求正則表達式”,但我真的無法想象這一個。 我需要做的是找到一個簡單的正則表達式,它將匹配任何大於或小於3位的數據。 它也必須匹配字符。

只是一點解釋。 我試圖匹配任何不是電話號碼的標准區號的東西。 所以> 3 <包括字符。 我將其用於業務規則,並且已經與區號的正面版本匹配。

一次只能通過正則表達式傳遞一條記錄,因此不需要分隔符。

好的抱歉,這里有一些例子:

337   : does not match
123   : does not match
12    : does match
1     : does match
asd   : does match
as2   : does match
12as45: does match
1234  : does match

相反的情況非常簡單,只能是[0-9] {3}或[\\ d] {3}。

PS它在java中

現在這是“az”的解決方案(因為它似乎很常見):

^(?!\d{3})[a-z0-9]{3}$|^[a-z0-9]{1,2}$|^[a-z0-9]{4,}$

...這是真正的解決方案,它匹配除了三個全部數字的字符之外的所有內容:

^(?!\d{3}).{3}$|^.{1,2}$|^.{4,}$

http://regexr.com?358u9

因為我們只是檢查三種選擇,所以它非常自我解釋。

你可以這樣做

^(?:(?!(?<!\d)\d{3}(?!\d))[a-zA-Z\d])+$

在Regexr上看到它。

解釋

^                                # match the start of the string (not needed with the matches() method)
    (?:                          # start of a non capturing group
        (?!(?<!\d)\d{3}(?!\d))   # combined lookarounds, fails if there are 3 digits following with not a digit before and ahead of those 3 digits
        [a-zA-Z\d]               # match one ASCII letter or digit
    )+                           # repeat this at least once
$                                # match the end of the string (not needed with the matches() method)

使用\\ d {3}使用負面看法: http//www.regular-expressions.info/lookaround.html

暫無
暫無

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

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