簡體   English   中英

如何限制正則表達式中的字符范圍?

[英]How to limit the character range in regular expression?

可以說,

let sentence= "Dear user {#val#}{#val#} thanks"

{#val#} 是上句中的動態值。 這里可以代替 {#val#},但可以有任何值,但至少可以有 0 個字符,最多可以有 5 個字符。 所以我將 {#val#} 替換為。{0,5}。 除了 {#val#} 部分之外,我不需要考慮空格,所以我形成的正則表達式是,

let regex =  /^Dear\s*user\s*.{0,5}.{0,5} thanks$/i
let customermsg = "Dear user 1 2 thanks" //Should be valid
let customermsg1 = "Dear user 12345 6789 thanks" //Should be valid
let customermsg2 = "Dear user 123 5 6789 thanks" //Should be valid because space can also be considered as a character and for fist .{0,5} => 123 5 and for second .{0,5} => 6789
let customermsg3 = "Dear user 1 thanks" //Should pass 
let customermsg4 = "Dea r user 1 tha nks" // Should Pass since spaces are not considered in the static portion.

但是當我嘗試使用下面的測試時,

 regex.test(customermsg)

它完全相反。 即使我嘗試過以下方法,

let splitters=/{\\#val\\#}|((\\s\*))/gi;
sentence = sentence.replace(splitters, (x, y) => y ? y : ".(\\S{0,5})"); 

這將正則表達式返回為,

 /^Dear\s*user\s*.(\S{0,5}).(\S{0,5})\s*thanks$/

但這也沒有按預期工作。 我堅持這一點。 請幫我。

您需要根據以下內容檢查數字空格是否多次出現

"Dear user 1 Thanks" //應該失敗,因為只有一個值

因此,您的正則表達式很好,只是它不檢查數字空格是否存在多次

使用以下正則表達式

Dear\s*user\s*([\d]+[\s]+){2,5}\s*thanks$
  • ([\d]+[\s]+){2,5}\s*

    • 捕獲零到無限次之間的組匹配數字以及零到無限次之間的空白,至少兩次,最多五次。

([\d]+[\s]+){2,5}\s*部分確保一個數字至少出現兩次,因此字符串中的單個數字Dear user... thanks將失敗。

您可以在數字之前、之間和之后使用任意數量的空格

 let regex = /Dear\s*user\s*([\d]+[\s]+){2,5}\s*thanks$/i let customermsg = "Dear user 1 2 thanks" //Should be valid let customermsg1 = "Dear user 12345 6789 thanks" //Should be valid let customermsg2 = "Dear user 123 5 6789 thanks" //Should be valid because space can also be considered as a character and for fist.{1,5} => 123 5 and for second.{1,5} => 6789 let customermsg3 = "Dear user 1 thanks" //Should fail since only one value is there let customermsg4 = "Dear user 435 4523 thanks" // With many spaces console.log(regex.test(customermsg)); console.log(regex.test(customermsg1)); console.log(regex.test(customermsg2)); console.log(regex.test(customermsg3)); console.log(regex.test(customermsg4));

https://regex101.com/r/qBFSxq/1

這可能是解決它的一種方法: ^Dear\s*user(\s*\d)?(?(1)(.{1,5})(?=(\s{1,}))(?<.(\s{2}))(,{1,5}) thanks|\s*thanks)$

編輯:添加正則表達式正前瞻結構( (.{1,5})(?=(\s{1,})) ), https://www.regular-expressions.info/lookaround.html ,以及使用正則表達式中的條件( (\s*\d)?(?(1)... )和否定的后視( (?<.(\s{2}))(,{1,5})

您可以在比賽前對部分使用 s 捕獲組,並以單個非空白字符開始比賽。

在右邊斷言 1-9 個字符,然后是thanks 如果是這種情況,請至少匹配另一個非空白字符,然后再匹配thanks ,直到達到 Thanks 。

例如

 let regex = /^(Dear\s*user\s*)\S(?=.{1,9} thanks$)\s*\S.*(?= thanks$)/i; [ "Dear user 1 2 thanks", "Dear user 12345 6789 thanks", "Dear user 123 5 6789 thanks", "Dear user 1 thanks", "Dear user 1 thanks" ].forEach(s => console.log(s.replace(regex, (m, g1) => g1 + "{#val#}{#val#}")) );

或者,如果單個捕獲組只能有數字和空格

 let regex = /^(Dear\s*user\s*)\d(?=[ \d]*\d)[ \d]{1,9}(?= thanks$)/i; [ "Dear user 1 2 thanks", "Dear user 12345 6789 thanks", "Dear user 123 5 6789 thanks", "Dear user 1 thanks", "Dear user 1 thanks", "Dear user 12345 64789 thanks" ].forEach(s => console.log(s.replace(regex, (m, g1) => g1 + "{#val#}{#val#}")) );

據我了解,你想要

  • 兩個 val 將被相同的正則表達式替換
  • 要忽略的 val 之前的空格
  • 空間成為 val 的一部分

並且

  • val 不能以空格開頭

第一點是最棘手的,因為您需要確保第一個值不以空格開頭,而第二個值可以,但仍然是相同的正則表達式。 本質上,您在這里有兩個不同的要求。

我能想到的唯一解決方案是向前看,以確保 user 之后的可選空格之后的下一個字符是非空格字符。

/^Dear\s*user\s*(?=\S)(.{1,5}.{1,5}) thanks$/

https://regex101.com/r/AtUxDS/2

不確定我是否完全理解您的要求,但如果您希望在任何字母之間允許空格,您可以執行以下操作:

^\s*D\s*e\s*a\s*r\s*u\s*s\s*e\s*r\s*(?:.\s*){0,5}\s*(?:.\s*){0,5}\s*t\s*h\s*a\s*n\s*k\s*s\s*$

說明: \s*放置在每個字母之前和之后以允許空格。 對於字符范圍, (?:.\s*){0,5}用於在每個計數字符后允許空格。 ?:在開始時將其排除為捕獲組。)

演示: https://regex101.com/r/2ZAqui/1

暫無
暫無

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

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