0

This should be a simple question, but I cannot find the answer. I need is to replace my Regex Match with blank space. What is the Syntax for this? Here is my Expression:

Matcher alphaSourceMatch = Pattern.compile('(?mi)<\\/?\\w*\\b[^>]*>').matcher(alphaSource);

I would simply like to replace the pattern with ' '.

3
  • Do you have an example of the text that this pattern is used against? Maybe a before & after (as you would expect it)...
    – Mark Pond
    Sep 18, 2015 at 17:01
  • Its extremely long and cryptic. It is an HTML email. I know that this matcher works, it's just a matter now of replacing the matches with blank space...how is that written? @MarkPond Sep 18, 2015 at 17:02
  • @MarkPond Essentially it just removes the content of all tags in an HTML email...leaving me with the plain text Sep 18, 2015 at 17:07

1 Answer 1

3

You just need to append the .replaceAll(' ') method to your Matcher instance to replace every match with a space.

Matcher alphaSourceMatch = Pattern.compile('(?mi)<\\/?\\w*\\b[^>]*>').matcher(alphaSource);
String replacedText = alphaSourceMatch.replaceAll(' ');

Docs: Matcher.replaceAll(string)

3
  • Thank you! Straightforward enough. I will give it a try. Sep 18, 2015 at 17:09
  • Updated my example, since replaceAll returns a string.
    – Mark Pond
    Sep 18, 2015 at 17:11
  • Wow you're on it. I was about to ask! Sep 18, 2015 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.