简体   繁体   中英

How to do these type of string pattern matching

So if i was given a string like this:

  s1|s2|s3|s4:t1|t2|t3

and lets say first of all i want to separate all of 's' strings and then separate all the 't' strings.

how do i get rid of the delimiters like '|'and ':',

i know i can do basic charAt and subString but that seem like way too much work.

like i know the pattern: every string is seperated by the |is there not a way to extract strings using patterns?

can you guys show me some code please, or some hints on topics i should learn.

the way i am doing it is using multiple arraylist to hold the index's of the delimeitrs and then arraylist to store all the strings and if i want to perform an operation it would be even harder.

You may use the String.split function:

String[] parts = "s1|s2|s3|s4:t1|t2|t3".split("[:|]");
// parts = { s1, s2, s3, s4, t1, t2, t3 }

Note that the parts array may not have the same size depending on the input (for example, s1|s2 will give {s1, s2} .

To extract any parts which has any letter and any number right after it you can use pattern

[a-z]\d+

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