简体   繁体   中英

Regular Expression/String split

I am not as familiar with RegEx as I probably should be. However, I am looking for an expression(s) that matches a variant of values.

I have a list of values (about 30k of them total):

ABCD1234
EF56789
GH123456J
GH123456JK
LMN654987P

I need to be able to split the letters at the front, the number is the middle and the letters at the end into 3 different variables. The values have an undetermined amount of characters at the start, undetermined amount of numbers in the middle and undetermined number of letters at the end.

Any help is appreciated.

You can use a regex with capturing groups like this instead of splitting:

([A-Z]+)([0-9]+)([A-Z]*)

Working demo

Also if you want to match strings as case insensitive you can use the i flag.

Working demo

Match information:

MATCH 1
1.  [0-4]   `ABCD`
2.  [4-8]   `1234`
3.  [8-8]   ``
MATCH 2
1.  [9-11]  `EF`
2.  [11-16] `56789`
3.  [16-16] ``
MATCH 3
1.  [17-19] `GH`
2.  [19-25] `123456`
3.  [25-26] `J`
MATCH 4
1.  [27-29] `GH`
2.  [29-35] `123456`
3.  [35-37] `JK`
MATCH 5
1.  [38-41] `LMN`
2.  [41-47] `654987`
3.  [47-48] `P`

Additionally, if you don't want the empty content then you could use this regex:

([a-z]+)([0-9]+)([a-z]+)?

You could simply iterate over each line and split them using entire block of numbers as a delimiter.

When you include a capture group in the regex used to identify the delimiter, the delimiter is then included in the returned array.

string[] substrings = Regex.Split(originalString, @"([0-9]+)")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM