从文本中提取字符串和数字

时间:2016-05-25 16:17:26

标签: regex

我有像10E4558AA0_String1_String2_String3_0_100_12mo这样的字符串。我想提取string1string2以及0100
0_100也可以是0_1011_2526_7576_100,我想提取0 10,{{1}分别是},11 2526 75。我认为正则表达式可以工作,但我正在努力编写部分。

1 个答案:

答案 0 :(得分:0)

通过Split

由于您的字符串由_分隔命令,但这会因语言而异。

Javascript看起来像str.split("_"),可以在rosettacode.org找到更多用于标记字符串的语言示例

通过Regex Matching

要通过正则表达式匹配严格执行此操作,您可以添加一些字符串验证,如下面的正则表达式。

^([0-9a-f]+)_(string[^_]+)_(string[^_]+)_(string[^_]+)_([0-9]+)_([0-9]+)_([^_]+)$

Regular expression visualization

实施例

现场演示

https://regex101.com/r/yT4bR4/1

示例文字

10E4558AA0_String1_String2_String3_0_100_12mo

样本匹配

[0][0] = 10E4558AA0_String1_String2_String3_0_100_12mo
[0][1] = 10E4558AA0
[0][2] = String1
[0][3] = String2
[0][4] = String3
[0][5] = 0
[0][6] = 100
[0][7] = 12mo

解释

NODE                     EXPLANATION
----------------------------------------------------------------------
  ^                        the beginning of the string
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    [0-9a-f]+                any character of: '0' to '9', 'a' to 'f'
                             (1 or more times (matching the most
                             amount possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  _                        '_'
----------------------------------------------------------------------
  (                        group and capture to \2:
----------------------------------------------------------------------
    string                   'string'
----------------------------------------------------------------------
    [^_]+                    any character except: '_' (1 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )                        end of \2
----------------------------------------------------------------------
  _                        '_'
----------------------------------------------------------------------
  (                        group and capture to \3:
----------------------------------------------------------------------
    string                   'string'
----------------------------------------------------------------------
    [^_]+                    any character except: '_' (1 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )                        end of \3
----------------------------------------------------------------------
  _                        '_'
----------------------------------------------------------------------
  (                        group and capture to \4:
----------------------------------------------------------------------
    string                   'string'
----------------------------------------------------------------------
    [^_]+                    any character except: '_' (1 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )                        end of \4
----------------------------------------------------------------------
  _                        '_'
----------------------------------------------------------------------
  (                        group and capture to \5:
----------------------------------------------------------------------
    [0-9]+                   any character of: '0' to '9' (1 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )                        end of \5
----------------------------------------------------------------------
  _                        '_'
----------------------------------------------------------------------
  (                        group and capture to \6:
----------------------------------------------------------------------
    [0-9]+                   any character of: '0' to '9' (1 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )                        end of \6
----------------------------------------------------------------------
  _                        '_'
----------------------------------------------------------------------
  (                        group and capture to \7:
----------------------------------------------------------------------
    [^_]+                    any character except: '_' (1 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )                        end of \7
----------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
----------------------------------------------------------------------
相关问题