java - How to Perform Regular Expression in JTextArea of Swing -
java - How to Perform Regular Expression in JTextArea of Swing -
i want replace matched words given regular expression.i wrote code that.but,it replace particular action not performed.when select regular look click on replace all,it replace particular action,not replace given string/character. illustration input "\n",matched string/character replace new line action,not replace new line character.please check , help me.thank you. code:
public class regularexp extends javax.swing.jframe { jtextarea text; int i=0; undomanager undomanager = new undomanager(); public regularexp() { initcomponents(); text = new jtextarea(); replace.setenabled(false); } @suppresswarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="generated code"> private void initcomponents() { tp = new javax.swing.jtabbedpane(); jmenubar1 = new javax.swing.jmenubar(); filemenu = new javax.swing.jmenu(); open = new javax.swing.jmenuitem(); replace = new javax.swing.jmenuitem(); setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close); filemenu.settext("file"); open.settext("open"); open.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { openactionperformed(evt); } }); filemenu.add(open); replace.settext("replace"); replace.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { replaceactionperformed(evt); } }); filemenu.add(replace); jmenubar1.add(filemenu); setjmenubar(jmenubar1); javax.swing.grouplayout layout = new javax.swing.grouplayout(getcontentpane()); getcontentpane().setlayout(layout); layout.sethorizontalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addcomponent(tp, javax.swing.grouplayout.alignment.trailing, javax.swing.grouplayout.default_size, 400, short.max_value) ); layout.setverticalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addcomponent(tp, javax.swing.grouplayout.alignment.trailing, javax.swing.grouplayout.default_size, 279, short.max_value) ); pack(); }// </editor-fold> private void replaceactionperformed(java.awt.event.actionevent evt) { jdialog replace_dialog=new jdialog(regularexp.this); replace_dialog.settitle("replace"); jlabel find_label=new jlabel("find what"); final jtextfield find_tf=new jtextfield(10); jlabel replace_label=new jlabel("replace with"); final jtextfield replace_tf=new jtextfield(10); final jcheckbox regx=new jcheckbox("regular expression"); jbutton replaceall=new jbutton("replace all"); replace_dialog.setlayout(new gridbaglayout()); gridbagconstraints c = new gridbagconstraints(); c.insets = new insets(5, 5, 5, 5); c.gridx = 0; c.gridy = 0; c.anchor = gridbagconstraints.west; replace_dialog.add(find_label, c); c.gridx++; c.fill = gridbagconstraints.horizontal; c.weightx = 1; replace_dialog.add(find_tf, c); c.gridx = 0; c.gridy = 1; c.gridwidth = 2; replace_dialog.add(replace_label, c); c.gridx++; c.fill = gridbagconstraints.horizontal; c.weightx = 1; replace_dialog.add(replace_tf, c); c.gridx++; c.gridx++; c.gridwidth = 1; c.fill = gridbagconstraints.horizontal; replace_dialog.add(replaceall, c); c.gridx=0; c.gridy++; c.gridwidth = 1; c.fill = gridbagconstraints.horizontal; replace_dialog.add(regx, c); replace_dialog.setsize(400,400); replace_dialog.setlocationrelativeto(null); replace_dialog.pack(); replace_dialog.setvisible(true); replaceall.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { if(regx.isselected()){ int caret_pos=text.getcaretposition(); text.settext(text.gettext().replaceall(find_tf.gettext(),"\\"+ replace_tf.gettext())); text.setcaretposition(caret_pos); } else { int caret_pos=text.getcaretposition(); text.settext(text.gettext().replaceall(find_tf.gettext(), replace_tf.gettext())); text.setcaretposition(caret_pos); } } }); } private void openactionperformed(java.awt.event.actionevent evt) { final jfilechooser jc = new jfilechooser(); int returnval= jc.showopendialog(regularexp.this); string title; file file=null; if(returnval == jfilechooser.approve_option) file = jc.getselectedfile(); if (jc.getselectedfile()!= null) { bufferedreader br = null; stringbuffer str = new stringbuffer(""); seek { br = new bufferedreader(new filereader(file)); string line; while ((line = br.readline()) != null) { str.append(line + "\n"); } } grab (ioexception ex) { logger.getlogger(regularexp.class.getname()).log(level.severe, null, ex); } string t = str.tostring(); final jinternalframe internalframe = new jinternalframe("",true,true); title=file.getname(); text.setfont(new java.awt.font("miriam fixed", 0, 13)); internalframe.add(text); i+=1; internalframe.setname("doc "+i); jscrollpane scrollpane=new jscrollpane(text); internalframe.settitle(title); tp.add(internalframe); internalframe.add(scrollpane); internalframe.setvisible(true); text.settext(t); text.setcaretposition(0); replace.setenabled(true); } } public static void main(string args[]) { seek { (javax.swing.uimanager.lookandfeelinfo info : javax.swing.uimanager.getinstalledlookandfeels()) { if ("nimbus".equals(info.getname())) { javax.swing.uimanager.setlookandfeel(info.getclassname()); break; } } } grab (classnotfoundexception ex) { java.util.logging.logger.getlogger(regularexp.class.getname()).log(java.util.logging.level.severe, null, ex); } grab (instantiationexception ex) { java.util.logging.logger.getlogger(regularexp.class.getname()).log(java.util.logging.level.severe, null, ex); } grab (illegalaccessexception ex) { java.util.logging.logger.getlogger(regularexp.class.getname()).log(java.util.logging.level.severe, null, ex); } grab (javax.swing.unsupportedlookandfeelexception ex) { java.util.logging.logger.getlogger(regularexp.class.getname()).log(java.util.logging.level.severe, null, ex); } java.awt.eventqueue.invokelater(new runnable() { public void run() { new regularexp().setvisible(true); } }); } // variables declaration - not modify private javax.swing.jmenu filemenu; private javax.swing.jmenubar jmenubar1; private javax.swing.jmenuitem open; private javax.swing.jmenuitem replace; private javax.swing.jtabbedpane tp; // end of variables declaration }
the point is: in 1 of cases should utilize function replace()
instead of replaceall()
.
note both replace()
, replaceall()
functions replaces occurrences. difference between them replaceall()
uses regex, while replace()
don't.
so should utilize next code:
replaceall.addactionlistener(new actionlistener(){ @override public void actionperformed(actionevent e){ int caret_pos = text.getcaretposition(); if(regx.isselected()){ text.settext(text.gettext().replaceall( find_tf.gettext(), replace_tf.gettext())); } else{ text.settext(text.gettext().replace( find_tf.gettext(), replace_tf.gettext())); } text.setcaretposition(math.min(caret_pos,text.gettext().length())); } });
according comments, question not have regular expressions. want handle special characters \n
, \t
differently in 2 situations. i'll allow above explanation ones want utilize regular expression. also, i'll add together below, reply specific situation.
updated reply work in both scenarios you've described in comments:
replaceall.addactionlistener(new actionlistener(){ @override public void actionperformed(actionevent e){ int caret_pos = text.getcaretposition(); if(regx.isselected()){ text.settext(text.gettext().replace( find_tf.gettext().replace("\\n","\n").replace("\\t","\t"), replace_tf.gettext().replace("\\n","\n").replace("\\t","\t"))); } else{ text.settext(text.gettext().replace( find_tf.gettext(), replace_tf.gettext())); } text.setcaretposition(math.min(caret_pos,text.gettext().length())); } });
this code deals \n
, \t
. if want deal other special characters have add together other replaces. example: if want deal \r
need next code:
text.settext(text.gettext().replace( find_tf.gettext().replace("\\n","\n").replace("\\t","\t").replace("\\r","\r"), replace_tf.gettext().replace("\\n","\n").replace("\\t","\t").replace("\\r","\r")));
you can have list of java escape sequences here: http://docs.oracle.com/javase/tutorial/java/data/characters.html
\t
insert tab in text @ point. \b
insert backspace in text @ point. \n
insert newline in text @ point. \r
insert carriage homecoming in text @ point. \f
insert formfeed in text @ point. \'
insert single quote character in text @ point. \"
insert double quote character in text @ point. \\
insert backslash character in text @ point. java swing jtextarea replaceall
Comments
Post a Comment