Gabriel Bombardi
Gabriel Bombardi

Reputation: 57

Split with regex is not splitting every match

I need to split a string on every match of #ABC3#, #ABC3_3#, or #ABC3_33# tags, so then I built the following regex:

/#([A-Z][A-Z][A-Z])([0-9])#|#([A-Z][A-Z][A-Z])([0-9])_([0-9])#|#([A-Z][A-Z][A-Z])([0-9])_([0-9][0-9])#/

And I want to split the following string:

1234 5647 8512 5648#EMB2#12#EMB3#DEC 12#EMB4#33/03#EMB7_12#02/12#EMB7_13#1999#EMB5#22/19#EMB6#DEC 22#EMB7_1#DEC 22#EMB7_2#14#EMB7_3#11/22#EMB7_14#11/22#EMB7_15#2013#EMB7_4#ASDCFG ASDFWED ODLSKEMT   #EMB7_5#423253 AB ASDCFGER ASDFGH#EMB7_6#4444#EMB7_8#222#EMB7_9##EMB7_16#                          

The problem is that the following code is not splitting every match of the string.

String[] embTagsToArray = embossingTags.split("/#([A-Z][A-Z][A-Z])([0-9])#|#([A-Z][A-Z][A-Z])([0-9])_([0-9])#|#([A-Z][A-Z][A-Z])([0-9])_([0-9][0-9])#/");

The final array was supposed to have 18 positions but it is being splitted in 9 array positions, like this:

[0] 1234 5647 8512 5648#EMB2#12#EMB3#DEC 12#EMB4#33/03#EMB7_12#02/12#EMB7_13#1999#EMB5#22/19#EMB6#DEC 22
[1] DEC 22
[2] 14
[3] 11/22#EMB7_14#11/22#EMB7_15#2013
[4] ASDCFG ASDFWED ODLSKEMT   
[5] 423253 AB ASDCFGER ASDFGH
[6] 4444
[7] 222
[8] #EMB7_16#   

Expected Output:

[0] 1234 5647 8512 5648
[1] 12
[2] DEC 12
[3] 33/03
[4] 02/12
[5] 1999
[6] 22/19
[7] DEC 22
[8] DEC 22
[9] 14
[10] 11/22
[11] 11/22
[12] 2013
[13] ASDCFG ASDFWED ODLSKEMT  
[14] 423253 AB ASDCFGER ASDFGH
[15] 4444
[16] 222
[17]     

I can't find what I'm doing wrong. Anyone?

Upvotes: 0

Views: 145

Answers (3)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89557

No need to do more:

String[] embTagsToArray = embossingTags.split("#[^#]*#");

(remove the last item if it is empty)

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174706

Just split your input string according to the below regex which uses a non-capturing group.

String s = "1234 5647 8512 5648#EMB2#12#EMB3#DEC 12#EMB4#33/03#EMB7_12#02/12#EMB7_13#1999#EMB5#22/19#EMB6#DEC 22#EMB7_1#DEC 22#EMB7_2#14#EMB7_3#11/22#EMB7_14#11/22#EMB7_15#2013#EMB7_4#ASDCFG ASDFWED ODLSKEMT   #EMB7_5#423253 AB ASDCFGER ASDFGH#EMB7_6#4444#EMB7_8#222#EMB7_9##EMB7_16#";
String[] tok = s.split("#[A-Z]{3}\\d(?:_\\d{1,2})?#");
System.out.println(Arrays.toString(tok));

Output:

[1234 5647 8512 5648, 12, DEC 12, 33/03, 02/12, 1999, 22/19, DEC 22, DEC 22, 14, 11/22, 11/22, 2013, ASDCFG ASDFWED ODLSKEMT   , 423253 AB ASDCFGER ASDFGH, 4444, 222]

DEMO

Regular Expression:

#                        '#'
[A-Z]{3}                 any character of: 'A' to 'Z' (3 times)
\d                       digits (0-9)
(?:                      group, but do not capture (optional):
  _                        '_'
  \d{1,2}                  digits (0-9) (between 1 and 2 times)
)?                       end of grouping
#                        '#'

Upvotes: 1

vks
vks

Reputation: 67968

String[] embTagsToArray = embossingTags.split("#([A-Z][A-Z][A-Z])([0-9])#|#([A-Z][A-Z][A-Z])([0-9])_([0-9])#|#([A-Z][A-Z][A-Z])([0-9])_([0-9][0-9])#");

This is working fro me.See demo.

http://regex101.com/r/oE6jJ1/33

Upvotes: 1

Related Questions