java 8 - Applying regex to Java8 List<String> -
i have array list
list<string> items = arrays.aslist("date=2017-01-10", "date=2017-02-10");
i want convert list , have quotes each item in list date=2017-01-10
can converted date='2017-01-10'
list<string> items = arrays.aslist("date='2017-01-10'", "date='2017-02-10'");
how can in java 8 ?
update: want use regex achieve this.
i have seen profile, found start java. i'll give answer learning purpose.
- use lookbehind skip replacement. e.g:
(?<==)
- use group capture matched result in group, e.g:
(.+)
use
$
sign references captured group, e.g:$1
.list<string> result = items.stream() // match `=` ---v .map(it -> it.replacefirst("(?<==)(.+)", "'$1'")) // capture rest substring in group ---^ .collect(collectors.tolist());
you can use string#substring achieve way. example:
int prefix = "date=".length(); list<string> result =items.stream() .map(it->string.format("date='%s'",it.substring(prefix))) .collect(collectors.tolist());
Comments
Post a Comment