java - How to convert a string from one pattern to another using regex? -
i have string want convert using regex:
aa_bb_cc_dd
=> ee_bb_ff_dd
tried using regex (aa)(.*)(ff)
. did not work. can help?
also nice if can point me regex guide. there many sites regex. not sure refer.
how about
string before = "aa_bb_cc_dd"; string after = before.replacefirst("aa(_bb_)cc(_dd)", "ee$1ff$2"); system.out.println(after); // ee_bb_ff_dd
you haven't described how form of input string vary, difficult produce suitable regex.
if wanted allow between aa
, cc
use (.*?)
instead of (_bb_)
etc.
the above shows principle of using in replacement string content captured ()
, i.e. $1
refers content in first ()
, , $2
second ()
.
links:
regular-expressions.info.
the java tutorials. lesson: regular expressions.
Comments
Post a Comment