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の"""
のように思えるが、我々はまた、コメントを書き込むことができます'''
を使用しています。
正規表現の補間子 'r"パターン "は、ユニットを空の文字列として補間し、' abc $ {} "を' 'abc()"。r'の代わりに '' abc ".r'に変換することができます。 –