1

I have the following string:

"Text before the string I want to get. Description: This is the string I want to keep. Rating: 5 stars."

From this string I want to cut of the first part and the last part. Only the middle is what I need. The program I use to load these strings can do a RegEx "Search and Replace". So I can replace the first and last part of the string with a space to empty these parts.

The words Description and Rating are always present in this form, so can be used to create the RegEx.

1
  • 3
    Which ones have you tried? Otherwise we won't be able to help you with learning… Commented Apr 29, 2013 at 21:06

2 Answers 2

5

Try replacing matches of the following regex with an empty string (or a space):

^.*?Description:|Rating:.*$

This may have some unexpected behavior if "Text before the string I want" can also contain "Description:" or the string you want to keep can contain "Rating:".

Note that technically the anchors (^ and $) are not necessary here, but they are still useful to make it clear that everything from the beginning of the string up to "Description:" is being removed, as well as everything from "Rating:" until the end of the string.

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

1 Comment

Works as expected, although I had to add a ^ as closing character.. Thanks!
1

if your tool/programming supports look-around:

'(?<=Description: ).*(?=Rating: )'

will give you the middle part. so that you don't have to "replace". just extract the matched part.

1 Comment

This is a nice alternative, although it is not supported by the tool I am using.

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.