簡體   English   中英

如何使 BIRT REGEX MATCH 語法正確

[英]How to get BIRT REGEX MATCH syntax correct

我試圖確保給定的 Birt 參數恰好包含一個、兩個或三個 4 位數年份 csv。 所以這些會很好:

2017
2017,2018
=2017
=   2017,2018,   =  2019
2019,2020,2021

這些會很糟糕

abc
2019,2018,2017,2016
2019,dddd,2020

我試過這個:

if(BirtComp.test(params["Fiscal Year"].value,"^ *?=? *?\d\d\d\d *?=? *?(, *?=? *?\d\d\d\d *?=? *?)?(, *?=? *?\d\d\d\d *?=? *?)?$"))
{
yearsValid=true;
} else {
yearsValid=false;
}

但即使參數只有 4 位數字,例如 2020,它也永遠不會返回 true。

我在數據集的打開腳本中執行此操作。 它似乎在記事本++中工作正常

作為這里的另一個數據點,這些都無法在我試圖放在數據集的 OPEN 底部的 Birt 中的 javascript 中編譯..請注意,“試試這個”在記事本++中效果很好:

//try this  ^=? *\d{4}(?:[, =]+\d{4}){0,2}$  

//generalDebug01 = BirtComp.test(params["Fiscal Year"].value,BirtComp.test(params["Fiscal Year"].value,"\\d\\d" )); 

//generalDebug01 = BirtComp.test(params["Fiscal Year"].value,BirtComp.test(params["Fiscal Year"].value,/\d\d/ )); 

//generalDebug01 = BirtComp.test(params["Fiscal Year"].value,BirtComp.test(params["Fiscal Year"].value,/\\d\\d/ )); 

//generalDebug01 = BirtComp.test(params["Fiscal Year"].value,BirtComp.test(params["Fiscal Year"].value,/^=? *\d{4}(?:[, =]+\d{4}){0,2}$/ )); 

//generalDebug01 = BirtComp.test(params["Fiscal Year"].value,BirtComp.test(params["Fiscal Year"].value,/^=? *\\d{4}(?:[, =]+\\d{4}){0,2}$/ )); 

有人可以指出我做錯了什么嗎? 這與其說是一個正則表達式問題,不如說是一個 Birt javascript 正則表達式問題。

您可以使用此正則表達式進行驗證:

^\W*\d{4}(?:\W+\d{4}){0,2}$

正則表達式演示

正則表達式詳情:

  • ^ : 開始
  • \\W* : 匹配 0 個或多個非單詞字符
  • \\d{4} : 匹配 4 位數字
  • (?:\\W+\\d{4}){0,2} :匹配 0 到 2 個由 1 個以上非單詞字符分隔的 4 位數字的實例
  • $ : 結束

僅根據您的示例,此正則表達式也可能適用於您:

^=? *\d{4}(?:[, =]+\d{4}){0,2}$

暫無
暫無

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

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