Search code examples
regexsublimetext3

How to use a regex to find and replace the beginning and end of a string


I've a lot of strings like this:

<string name="minutes">minutes</string>
<string name="hours">hours</string>
<string name="zero_minutes">None</string>

I want to get only values:

minutes
hours
None

I wanted to do a regular expression to find all the ones that start with <string and end with "> and replace with a blank space. How can I do this?

I went through multiple topics of this, but my expressions do not work:

com\/\K<string(\/.*?)\">

Solution

  • Find

    <string[^>]*>([^<]*)</string>
    

    Replace

    $1
    

    Note that this regex is fragile and will not account for whitespaces (e.g. < string>) or carets inside quotes (e.g. <string name=">">). It's best to use a parser.