2016-08-28 9 views
1

正規表現を書くとき、私は新しい行にステップによってScalaのトリプル引用文字列と

EX私の正規表現のステップを書くことができますので、私はスカラ座の"""構文は非常に便利見つけるコメント:私が望むしかし

val foo = 
""" 
    (
    |(
    |\d{3} 
    || 
    |\(\d{3}\) 
    |)? 
    |(
    |\s|-|\. 
    |)? 
    |\d{3} 
    |(\s|-|\.) 
    |\d{4} 
    |(
    |\s* 
    |(
    |ext|x|extn|extn. 
    |) 
    |\s* 
    |\d{2,6} 
    |)? 
    |)""".stripMargin.replace("\n", "").r 

を私は

val foo = 
    """(      // start group to capture the phone number 
     |(      // start of optional area code choices 
     |\d{3}     // bare three digits 
     ||      // or 
     |\(\d{3}\)    // three digits enclosed in parentheses 
     |)?      // end of optional area code choices 
     |(      // start of optional separator 
     |\s|-|\.     // start of optional separator 
     |)?      // separator can be whitespace, dash or period 
     |\d{3}     // exchange number (required) 
     |(\s|-|\.)    // same separator but required this time 
     |\d{4}     // final digits (required) 
     |(      // start of optional extension 
     |\s*      // zero or more characters of white space 
     |(      // start of extention indicator 
     |ext.|x.|ext.|extn.  // extention can be indicated by "ext", "x", or extn followed by any character 
     |)      // end of extension indicator 
     |\s*      // zero or more characters of white space 
     |\d{2,6}     // two to five digits of extension number 
     |)?      // end of optional estension 
     |)""".stripMargin.replace("\n", "").trim 
    println(foo) 
    val regex = foo.r 
    val input = "(888)-456-7890 extn: 12345" 
    regex.findAllIn(input).foreach(println) 

のように各行にやっているかを説明するコメントが書かれている可能性がしかし、Scalaはコメント文字列自体の一部になること。だから、どのように私は、コメントを書くことができ、ここのpythonで

verboseRegex = re.compile(r''' 
    (   # start group to capture the phone number 
    (   # start of optional area code choices 
    \d{3}   # bare three digits 
    |    # or 
    \(\d{3}\)  # three digits enclosed in parentheses 
    )?   # end of optional area code choices 
    (   # start of optional separator 
    \s|-|\.  # separator can be whitespace, dash or period 
    )?   # end of optional separator 
    \d{3}   # exchange number (required) 
    (\s|-|\.)  # same separator but required this time 
    \d{4}   # final digits (required) 
    (   # start of optional extension 
    \s*   # zero or more characters of white space 
    (   # start of extention indicator 
    ext|x|ext. # extention can be indicated by "ext", "x", or 
        #  "ext" followed by any character 
    )    # end of extension indicator 
    \s*   # zero or more characters of white space 
    \d{2,5}  # two to five digits of extension number 
    )?   # end of optional estension 
    )    # end phone number capture group 
    ''', re.VERBOSE) 

そこで上記のPythonコードのように、複数行の文字列は、私たちのScalaの"""のように思えるが、我々はまた、コメントを書き込むことができます'''を使用しています。

答えて

2

どうやら、(?x)は無視して空白やコメントをサポートしています。

scala> val r = """(?x)abc 
    | # works ok 
    | def""".r 
r: scala.util.matching.Regex = 
(?x)abc 
# works ok 
def 

scala> "abcdef" match { case r(_*) => } 

scala> val r = s"""(?x)abc\n |def #works, I hope\n |123""".stripMargin.r 
r: scala.util.matching.Regex = 
(?x)abc 
def #works, I hope 
123 

scala> "abcdef123" match { case r(_*) => } 

もう一つのアイデア:

scala> val r = s"abc${ "" // comment this 
    | }def${ "" // not pretty 
    | }".r 
r: scala.util.matching.Regex = abcdef 

scala> "abcdef" match { case r(_*) => } 

これらの穴に空の文字列を返すcomment"interpolator"を持っていると便利かもしれません。

scala> val r = s"abc${ comment"empty words here" }".r 

あなたはキャプチャグループを無視した場合、余分な括弧は気にしないで、次のとおりです。

scala> val r = s"abc${ // comment 
    | }".r 
r: scala.util.matching.Regex = abc() 

scala> "abc" match { case r(_*) => } 

それはそれはユニットの代わりに、空の文字列を挿入残念です。

+0

正規表現の補間子 'r"パターン "は、ユニットを空の文字列として補間し、' abc $ {} "を' 'abc()"。r'の代わりに '' abc ".r'に変換することができます。 –

関連する問題