简体   繁体   中英

How to remove all occurrences of a substring from a string

I have a String(txt) and in this String is html code. I will search the String with txt.indexOf for ("< ac:structured-macro ac:macro-id=")

and delet it with StringBuffer(sb) sb.delete(Index, EndIndex). I will do this multiple times but when i do this with a while Loop it dosen't work and find only the index of the first element("ac:structured-macro ac:macro-id=").

Edit: The Main Problem, is that the id is always diffrent and i want to delet it too.

String txt = textArea1.getText(); 

/*somthing like this <p>
<p>
  <br/>
</p>
<ac:structured-macro ac:macro-id="74563a55-dc09-41a1-acaa-7c6338ab4014" ac:name="unmigrated-wiki-markup" 
 ac:schema-version="1">
  <ac:plain-text-body>
    <![CDATA[
      Lorem ipsum dolor sit amet, consetetur sadipscing elitr
    ]]>
  </ac:plain-text-body>
</ac:structured-macro>
<p>
  <br/>
</p>
<ac:structured-macro ac:macro-id="bc7e6c08-82c8-4ee9-8582-b773914857f" ac:name="unmigrated-wiki-markup" 
 ac:schema-version="1">
  <ac:plain-text-body>
    <![CDATA[
      Lorem ipsum dolor sit amet, consetetur sadipscing elitr
    ]]>
  </ac:plain-text-body>
</ac:structured-macro>
*/


int StartIndexOfMacroID = 0;
int indexEndOfMacroID = 0;
StringBuffer sb = new StringBuffer(txt);

while (StartIndexOfMacroID != -1) {
       StartIndexOfMacroID = txt.indexOf("<ac:structured-macro ac:macro-id=");
       indexEndOfMacroID = StartIndexOfMacroID + 159;
       sb.delete(StartIndexOfMacroID, indexEndOfMacroID);
       System.out.println(StartIndexOfMacroID);
       );

txt = sb.toString();

System.out.println(StartIndexOfMacroID);
System.out.println(indexEndOfMacroID);

textArea2.setText(txt);

This is the output:


17
176
159
318
Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: start -1, end 158, length 282
    at java.base/java.lang.AbstractStringBuilder.checkRangeSIOOBE(AbstractStringBuilder.java:1724)
    at java.base/java.lang.AbstractStringBuilder.delete(AbstractStringBuilder.java:863)
    at java.base/java.lang.StringBuffer.delete(StringBuffer.java:474)
    at com.company.Main$1.actionPerformed(Main.java:115)
    at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
    at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
    at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
    at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
    at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:270)
    at java.desktop/java.awt.Component.processMouseEvent(Component.java:6651)
    at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
    at java.desktop/java.awt.Component.processEvent(Component.java:6416)
    at java.desktop/java.awt.Container.processEvent(Container.java:2263)
    at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5026)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4858)
    at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
    at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547)
    at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
    at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2772)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4858)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:778)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:727)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:751)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:749)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:748)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

Thank You for your help!

Basically, the problem is to remove all occurrences of a certain html tag in the original string.

In java it may be accomplished with far less effort than your approach requires:

final String htmlContent = textArea1.getText();
final String filteredHtmlContent = htmlContent.replaceAll("<ac:structured-macro.*?</ac:structured-macro>", "");

This code replaces all occurrences of a given string with an empty string, which is equivalent to removing them.

Unfortunately, the code you posted isn't compiling. Thus, it doesn't make sense to analyze the reason it isn't working correctly. I strongly suggest to review it before sending it to stackoverflow community.

I think you would like to remove only the tag. Because you have some line breaks

String patternText = "(<)(/)?ac:structured-macro((?!>).)*>";
Pattern pattern = Pattern.compile(patternText, Pattern.DOTALL);
String result = pattern.matcher(txt).replaceAll("");
System.out.println(result);

Explanation for the regexp:

  • The tags starts with (<) .
  • You have a opening tag and a closing tag, so we need optional (/)?
  • Next we have the literal ac:structured-macro
  • Than we search for everything execpt > over multiple lines with ((?.>).)*
  • Last we need the closing > with >

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM