使用Scala分组提取

时间:2018-10-15 06:02:22

标签: regex scala

让我说我有代码:

C:\Python27\python.exe E:/PycharmProjects/my_project/utils/encryption.py
Traceback (most recent call last):
  File "E:/PycharmProjects/my_project/utils/encryption.py", line 3, in <module>
    cipher_suite = Fernet(key)
  File "C:\Python27\lib\site-packages\cryptography\fernet.py", line 32, in __init__
    backend = default_backend()
  File "C:\Python27\lib\site-packages\cryptography\hazmat\backends\__init__.py", line 15, in default_backend
    from cryptography.hazmat.backends.openssl.backend import backend
  File "C:\Python27\lib\site-packages\cryptography\hazmat\backends\openssl\__init__.py", line 7, in <module>
    from cryptography.hazmat.backends.openssl.backend import backend
  File "C:\Python27\lib\site-packages\cryptography\hazmat\backends\openssl\backend.py", line 16, in <module>
    from cryptography import utils, x509
  File "C:\Python27\lib\site-packages\cryptography\x509\__init__.py", line 8, in <module>
    from cryptography.x509.base import (
  File "C:\Python27\lib\site-packages\cryptography\x509\base.py", line 16, in <module>
    from cryptography.x509.extensions import Extension, ExtensionType
  File "C:\Python27\lib\site-packages\cryptography\x509\extensions.py", line 24, in <module>
    from cryptography.x509.general_name import GeneralName, IPAddress, OtherName
  File "C:\Python27\lib\site-packages\cryptography\x509\general_name.py", line 18, in <module>
    from cryptography.x509.name import Name
  File "C:\Python27\lib\site-packages\cryptography\x509\name.py", line 28, in <module>
    _ASN1_TYPE_TO_ENUM = dict((i.value, i) for i in _ASN1Type)
  File "C:\Python27\lib\site-packages\cryptography\x509\name.py", line 28, in <genexpr>
    _ASN1_TYPE_TO_ENUM = dict((i.value, i) for i in _ASN1Type)
AttributeError: 'int' object has no attribute 'value'

Process finished with exit code 1

如何提取从val data = """^0001|1|0|813|1|860152033876093|!^0002|1|0|813|1|860152033876093|!^0003|1|0|813|1|860152033876093|!""" val pattern = """^\\^(\\w|)!$""".r pattern.findAllIn(data).foreach(println) 开始并以^结尾的组文本 就像我需要如下结果:

!

1 个答案:

答案 0 :(得分:-1)

这对您有用吗?

scala> val data = """^0001|1|0|813|1|860152033876093|!^0002|1|0|813|1|860152033876093|!^0003|1|0|813|1|860152033876093|!"""
data: String = ^0001|1|0|813|1|860152033876093|!^0002|1|0|813|1|860152033876093|!^0003|1|0|813|1|860152033876093|!

scala> val pattern = """\^[^!]+?!""".r
pattern: scala.util.matching.Regex = \^[^!]+?!

scala> pattern.findAllIn(data).foreach(println)
^0001|1|0|813|1|860152033876093|!
^0002|1|0|813|1|860152033876093|!
^0003|1|0|813|1|860152033876093|!

scala>