Split complex string

I have a line like this:

1|f1|</a1|a2/></a3|a4/>|f2 

I want to divide by '|' by java but I need to ignore </ and /> . How can i do this? Sounds like a regexp approach in this

the above line should be divided by:

1

f1

a1 | a2

a3 | a4

f2

+6
source share
3 answers

split method uses regex as a parameter, and | in regex, a special character that means OR. To make it a normal \\ character before he likes it

 "yourString".split("\\|"); 

In your case, you'll need look-ahead , so your regular expression might look like

 /></|(/>)?\\|(?=[^>]*(</|$))(</)? 

It will be broken into

  • /></
  • | with optional /> before or </ after it, BUT ONLY if there isnโ€™t > after it before </ or the end of your $ input. This ensures that | is out </ />

Also, to get rid of problems in situations like "</a|b/>|c|</d|e/>" , where </ is at the beginning and /> at the end of your input, you need to delete them before separation.

This seems necessary because we do not want to create an empty string as the first or last element in the created array, as in the case of "ab".split("a") , which will produce {"", "b"}

Allows you to check it:

 for (String s : "</a0|b0/>|1|f1|</a1|a2/></a3|a4/>|f2|</a5|a6/>" .replaceAll("^</", "").split("/></|/>$|(/>)?\\|(?=[^>]*(</|$))(</)?")) { System.out.println(s); } 

output:

 a0|b0 1 f1 a1|a2 a3|a4 f2 a5|a6 
+3
source

You can try the following Regex, which uses a negative look ahead .

 (?!</[^\|]*)[\|](?![^\|]*/>) 

This works like:

[\|] matches occurrences |

(?!</[^\|]*) indicates that matches should not be preceded by </sometext

(?![^\|]*/>) indicates that sometext/> does not follow these matches

Note: in the above sometext example, sometext are zero or more characters that are not |

+2
source

this regular expression must match. imma leave a list of possible things to try if one fails to move on to the next. The first of these, \ b, should avoid the word, but java may not be able to escape the backslash, so I added the second. If both of them do not pass to the last. This suggests that this should be a letter between capital A and common z. Now there should be no options for any spaces that have ever been.

The end result for the latter is a match:

"<" any character, multiple times, ">", if that fails, then

match:

any character that is a letter or number

 "(<.*?>|[^|\\b]*)" "(<.*?>|[^|\b]*)" "(<.*?>|[A-z0-9]*)" public String[] methodName(String s) { ArrayList<String>list= new ArrayList<String>(); Pattern p=Pattern.compile("(<.*?>|[^|]*)"); Matcher match=p.matcher(s); while(match.find()) { list.add(match.group()); } String[] listArray= new String[list.size()]; return listArray.toArray(listArray); } 

don't forget to vote if this helps cheers mate

+1
source

All Articles