Copy array using Regular Expression in Java -
i'm trying re-create string array string array. re-create has contain parts of each string of original array.
for example, if have
string[] originalarray = {"/data/test2/", "/data/test4/", "/data/dropbox/test5/"}
i want re-create array be
string[] copyarray = {"test2", "test4", "test5"}
my solution iterate through original array , utilize regular look grab lastly part of string in originalarray , create copyarray consisting of values.
is above method valid, or there more efficient solution this? regular look utilize case? way i'm doing seems bit brute forced.
ideally, manually create copyarray, in case, size of originalarray , precise content unknown.
edit:
this seems trivial reason it's not working.
i added regular expression. seems work in tester it's not working wanted in program. first converted originalarray string | appended regex.
string pattern = "/\\w+(?=\\||$)/g"; string testarray = originalarray.replaceall(pattern," ");
however test array giving me original concatenated string without regex applied.
up java 7, need code loop, java 8 introduced streams allow fluent one-line solution:
string[] names = arrays.stream(originalarray) .map(s -> s.replaceall(".*/", "")) .toarray();
the of import bit lambda expression convert path name using regex replace , including lastly slash blank (effectively removing it).
java arrays regex algorithm
No comments:
Post a Comment