-1

Let's say I want to match the phrase 'Johnny Appleseed' and we are given the following string:

<p>Johnny random blah stuff</p>
<p>more random stuff Appleseed blah blue bleh</p>

I would like to be able to match Johnny and Appleseed in order to wrap those words in a tag, how can I go about this?

Expected output:

<p><span>Johnny</span> random blah stuff</p>
<p>more random stuff <span>Appleseed</span> blah blue bleh</p>

Thanks for all advice!

Note: Basically what I want is what this page is automatically doing with the words Johnny and Appleseed in the example string (its making Johhnny and Appleseed be different colors than the other text)

BlahMclean
  • 11
  • 6
  • 1
    Regexes are used for looking for patterns within a string. looking at your example you just want to concatenate the three strings `
    `, original, `
    `. If this is not so please rewrite the question to give a better example.
    – JGNI Aug 06 '18 at 15:28
  • You might want to [reconsider using regex on html](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454), especially if you don't know what structure your code can have. To make it short: _regular_ expressions are no good fit for _irregular_ problems as yours. While the problem you describe in the question might seem somewhat easy to handle I have the feeling that your actual problem is (or will be) a lot more complex and thus my pledge: think of another solution. We're glad to help but we probably need a lot more info. – Thomas Aug 06 '18 at 16:16
  • I see. I do always see 'regex is not good for html' but I never see anyone post non-regex solutions on threads like that. It can be pretty hard to think of non-regex solutions for regex-like scenarios. I'll definitely open my thinking up to non-regex solutions however – BlahMclean Aug 06 '18 at 17:11
  • Edited the question to be more specific – BlahMclean Aug 06 '18 at 17:53
  • What is the expected output for your example? Do you want to wrap each word? – streetturtle Aug 06 '18 at 18:00
  • edited to add expected output. I know that there are ways I can grab groups out of a regex matcher using $0 or $1 notation. I just need help getting the matches to happen – BlahMclean Aug 06 '18 at 18:13

1 Answers1

0

Decided to go with Jsoup to parse through my HTML instead of using regex. Thanks for time!

BlahMclean
  • 11
  • 6