LoginSignup
1
1

More than 5 years have passed since last update.

0から任意の数までにマッチする正規表現

Last updated at Posted at 2017-03-04

ここで紹介されていたこれをGroovyに移植したものです。
ソースコードはここにあります。
file.groovyはそのまま忠実に移植したもので、shorterRegex.groovy(...|...)で少し短縮したものを出力するものです。
入力はstdinにして下さい。

改善できそうな所

文字列のそれぞれの文字をintとしてバラす

...
def l=num.length()-1
def n=new int[l+1]

for(def i=0;i<=l;i++){
    n[i]=new Integer("${num.charAt(i)}")//ここでバラしている
    ...
}

もうちょっと綺麗にできそうですが...

出力

1234567890をこれに入力すると
^\d{1,9}$|^1[0-1]\d{8}$|^12[0-2]\d{7}$|^123[0-3]\d{6}$|^1234[0-4]\d{5}$|^12345[0-5]\d{4}$|^123456[0-6]\d{3}$|^1234567[0-7]\d{2}$|^12345678[0-8]\d$|^1234567890$
を出力するのですが、これも(...|...)でまとめられそうな気がします。でも面倒くさい

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1