user1061692
user1061692

Reputation: 53

A regular expression to match a set of strings

I need to match a set of strings in Java. This string can contain self ending HTML, one or more white spaces and one or more  s.

For example:

String html = "<p>Stack Overflow is a great site. I really like Stack<br/>Overflow. Stack&nbsp;&nbsp;Overflow has helped me a lot to learn different things. I frequently visit Stack<br></br>Overflow. Stack<div id=\"XX\" />Overflow is really nice.<p><br/><p>Stack and overflow are two different thing.</p>".

Now I need a regular expression which would match the following strings in the above string.

 1. Stack Overflow 
 2. Stack<br/>Overflow
 3. Stack&nbsp;&nbsp;Overflow
 4. Stack<br></br>Overflow
 5. Stack<div id=\"XX\" />Overflow

But it shouldn't match

Upvotes: 3

Views: 7481

Answers (2)

publicRavi
publicRavi

Reputation: 2763

stack(<.*?>|&nbsp;|\s)*overflow

Upvotes: 2

Gowtham
Gowtham

Reputation: 1475

If I understand your question correctly, you are looking to match "stack" followed by "overflow" with allowing some optional text between them. If this is what you want, how about this:

(?i)stack.*?overflow

This will not behave very well if your input string contains "stack" but no corresponding "overflow".

You can learn more about java's regular expression syntax @ http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html

Upvotes: 1

Related Questions