java - Regexes for modifying beginning and end of line or string -
given string 1990january20hello.abc want apply regexes final string 1990january-20hello.abc
i thought do:
string text = "1990january20hello.abc"; pattern p = patter.compile("(.*)(january|jan)(.*)", pattern.case_insensitive | pattern.dotall); matcher m = p.matcher(text); while (m.find()){ string val1 = m.group(2); string val2 = val1.replace("$", "-"); text = text.replace(val1, val2); }
when seems in while loop find "january" val2 , text stay january. doing wrong? if java doesn't recognize $ end of line/string. ideally want val1.replace("(^|$)","-") can 1990-january-20hello.abc final string. please help. suggestions in advance.
use pattern:
(\d+)([a-za-z]+)(\d+)(.*)
the resulting groups , values are:
group 1 = 1990, group 2 = january, group 3 = 20, group 4 = rest of string.
and can append them , add whatever want.
Comments
Post a Comment