0

I'm trying to strip content titles out of the middle of text strings. Could I use regex to strip everything out of this string except for the title (in italics) in these strings? Or is there a better way?

Joe User wrote a blog post called The 10 Best Regex Expressions in the category Regex.

Jane User wrote a blog post called Regex is Hard! in the category TechProblems.

I've tried to come up with a regex expression to cover this, but I think it might need two. The trick is that the text in bold is always the same, so you could search for that, like this:

regex: delete everything before and including wrote a blog post called

regex: delete in the category and everything after it.

2
  • 1
    so, what have you tried so far? Commented Apr 21, 2010 at 12:46
  • horrible, amateur regex code that should not see the light of day. :-) correct answer below. Commented Apr 21, 2010 at 14:09

1 Answer 1

1
^.+ wrote a blog post called (.+) in the category .+$

So:

preg_replace( '|^.+ wrote a blog post called (.+) in the category .+$|', '\1', $str );

^ is start of line
.+ is some characters (one to many)
(.+) same, but the content will be available in \1, for preg_replace()
$ is the end of line

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.