2016-05-25 34 views
-2

私は10E4558AA0_String1_String2_String3_0_100_12moのような文字列を持っています。私はstring1string2、また0100を引き出したいと思います。
0_1000_1011_2526_75または76_100ことができ、私はそれぞれ、0 1011 2526 7576 100を引き出したいと思います。私は正規表現が動作すると思いますが、私はコーディング部分で苦労しています。テキストから文字列と数字を抽出する

+4

はいあなたは正規表現が動作するcorrect..aですが、あなたは何を試してみましたが?マッチ – rock321987

+0

引き出したいと言われるものはすべて、無関係です。 'string [12] | 0 | 100'と同じです – sln

+1

正規表現のフレーバー/プログラミング言語を詳しく説明してください。 – Jan

答えて

0
あなたの文字列が _ splitコマンドのいずれかによって区切られているので

スプリット経由

が、これは言語ごとに異なり。

Javascriptがstr.split("_")ようになり、文字列をトークン化のより多くの言語の例は、正規表現はあなたが追加することができます一致する正規表現を経由して厳密にこれを行うには

のマッチングを経てrosettacode.org


で見つけることができいくつかの文字列の検証は次の正規表現のようです。

^([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 
---------------------------------------------------------------------- 
関連する問題