I have strings like 10E4558AA0_String1_String2_String3_0_100_12mo
. I want to pull out string1
and string2
and also 0
and 100
.
The 0_100
could also be 0_10
, 11_25
, 26_75
or 76_100
and I'd want to pull out 0 10
, 11 25
, 26 75
, 76 100
, respectively. I think a regex would work but I am struggling with the coding part.
Asked
Active
Viewed 32 times
-2
-
4yes you are correct..a regex will work, but what have you tried? – rock321987 May 25 '16 at 16:18
-
Everything you mention you want to pull out is unrelated. It's equivalent to `string[12]|0|100` – May 25 '16 at 16:28
-
1Please elaborate on your regex flavor/programming language used. – Jan May 25 '16 at 16:30
-
What environment/language are you using? – May 25 '16 at 16:31
-
Possible duplicate of [Learning Regular Expressions](http://stackoverflow.com/questions/4736/learning-regular-expressions) – Biffen May 25 '16 at 17:37
1 Answers
0
via Split
Since your string is delimited by _
either a split command, but this varies from language to language.
Javascript would look something like str.split("_")
, and more language examples of tokenizing a string could be found at rosettacode.org
via Regex Matching
To do this strictly via regular expression matching you can add in some string validation like in the following regex.
^([0-9a-f]+)_(string[^_]+)_(string[^_]+)_(string[^_]+)_([0-9]+)_([0-9]+)_([^_]+)$
Example
Live Demo
https://regex101.com/r/yT4bR4/1
Sample text
10E4558AA0_String1_String2_String3_0_100_12mo
Sample Matches
[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
Explanation
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
----------------------------------------------------------------------

Ro Yo Mi
- 14,790
- 5
- 35
- 43