2016-05-25 2 views
-2

У меня есть строки, такие как 10E4558AA0_String1_String2_String3_0_100_12mo. Я хочу вытащить string1 и string2, а также 0 и 100.
0_100 также может быть 0_10, 11_25, 26_75 или 76_100, и я бы хотел, чтобы вытащить 0 10, 11 25, 26 75, 76 100 соответственно. Я думаю, что регулярное выражение будет работать, но я борюсь с кодирующей частью.Извлечь строки и цифры из текста

+4

да вы correct..a регулярное выражение будет работать, но что вы пробовали? – rock321987

+0

Все, что вы упомянули о том, что хотите вытащить, не связано. Это эквивалентно 'string [12] | 0 | 100' – sln

+1

Просьба подробно описать ваш язык повторного выражения/язык программирования. – Jan

ответ

0

через Сплит

Поскольку ваша строка ограничена _ либо команды раздельной, но это зависит от языка.

Javascript будет выглядеть как str.split("_") и другие примеры языковых tokenizing строки можно найти на 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 
---------------------------------------------------------------------- 
Смежные вопросы