Replacing with Regex

  • Hi all,

    since I’m very new to Notepad++ I could use some help with Regex.
    I was searching for a solution but cant find one for my issue - or in other words: I’m too noobish to adopt any solution :-)

    I have a code where I need to replace an old command with a new one.

    It’s build like this:
    tempChart.range = {[Value,Value]};

    And I need it like this:
    tempChart.setRange = ([Value,Value]);

    Can someone help me here?

    Thanks in advance

  • Welcome to the Notepad++ Community, @NA.

    You said:

    I have a code where I need to replace an old command with a new one.

    There are plenty of regexes that will change that specifically. It depends on how generic you need it to be. More examples of what lines should change and what lines shouldn’t would help.

    • Find What = (?-s)tempChart.range = \{(.*?)\}
    • Replace With = tempChart.setRange = \($1\)
    • Search Mode = Regular Expression

    I decided that the start-of-match was going to be specific (only looking for tempChart.range), but that you wanted to match whatever came between the { / } curly-brace pair, not just the literal [Value,Value] text.

    I’m guessing that those assumptions, and the regex that implements them, will get you close to what you want. If not, you’ll have to provide additional examples to show where it goes wrong.

    FYI: I often add this to my response in regex threads, unless I am sure the original poster has seen it before. Here is some helpful information for finding out more about regular expressions, and for formatting posts in this forum (especially quoting data) so that we can fully understand what you’re trying to ask:

    This forum is formatted using Markdown, with a help link buried on the little grey ? in the COMPOSE window/pane when writing your post. For more about how to use Markdown in this forum, please see @Scott-Sumner’s post in the “how to markdown code on this forum” topic, and my updates near the end. It is very important that you use these formatting tips – using single backtick marks around small snippets, and using code-quoting for pasting multiple lines from your example data files – because otherwise, the forum will change normal quotes ("") to curly “smart” quotes (“”), will change hyphens to dashes, will sometimes hide asterisks (or if your text is c:\folder\*.txt, it will show up as c:\folder*.txt, missing the backslash). If you want to clearly communicate your text data to us, you need to properly format it.

    If you have further search-and-replace (“matching”, “marking”, “bookmarking”, regular expression, “regex”) needs, study this FAQ and the documentation it points to. Before asking a new regex question, understand that for future requests, many of us will expect you to show what data you have (exactly), what data you want (exactly), what regex you already tried (to show that you’re showing effort), why you thought that regex would work (to prove it wasn’t just something randomly typed), and what data you’re getting with an explanation of why that result is wrong. When you show that effort, you’ll see us bend over backward to get things working for you. If you need help formatting, see the paragraph above.

    Please note that for all regex and related queries, it is best if you are explicit about what needs to match, and what shouldn’t match, and have multiple examples of both in your example dataset. Often, what shouldn’t match helps define the regular expression as much or more than what should match.

  • It worked perfectly!

    Thank you very much :)

Log in to reply