regex - Java - How to format a regular expression? -


i want create small window user :

  1. enter text in jtextfield1.
  2. then enter regular expression in jtextfield2 check if text in jtextfield1 has text matching regex.
  3. and field jtextfield3 can enter formating matching text
  4. the user final text matching , formatted in jtextfield4.

this seems complicated, hope screenshot clarify needs : enter image description here

implementing first 2 fields easy, third field, want ask about. have read references can me that, not clear me. understand if wrote \2, \1 appears in screenshot, grab second bracket in regex adds comma + space grabs first bracket in regex, giving right final result.

1. right, or missing ?

2. best solution implement need ?

3. after getting matcher in java code = returns "k1234 t1234567" code format using specified format ?

what have looks right. you'll need result formatted:

// inputs sake of working example snippet string text = "k1234 t1234567"; string query = "([a-z]{1}[0-9]{4})((\\ ){1})([a-z]{1}[0-9]{7})"; string format = "\\4, \\1";  // usual regex pattern matching pattern pattern = pattern.compile(query); matcher matcher = pattern.matcher(text); matcher.find();  // need tweak format use $ instead of \ replacement string replacement = format.replaceall("\\\\", "\\$"); string result = matcher.replaceall(replacement);  // outputs "t1234567, k1234" system.out.println(result); 

Comments