Sublime text Find Replace any Character

12,095

Solution 1

It appears that the MattDMo answer is not the correct pattern for the desired results. Here's what I did for a more elegant solution:

Open Sublime Text 2

Paste the block:

setI0100(mds3a1.getI0100().toString());
setI0200(mds3a1.getI0200().toString());
setI0300(mds3a1.getI0300().toString());
setI0400(mds3a1.getI0400().toString());
setI0500(mds3a1.getI0500().toString());
setI0600(mds3a1.getI0600().toString());
setI0700(mds3a1.getI0700().toString());
setI0800(mds3a1.getI0800().toString());
setI0900(mds3a1.getI0900().toString());
setI1100(mds3a1.getI1100().toString());
setI1200(mds3a1.getI1200().toString());
setI1300(mds3a1.getI1300().toString());

Find > Replace (CTRL+H)

Enable Regular expressions button (Alt+R)

Find What: \(.*

Replace With: [No characters here]

Press Replace All

Done

The result is what you were asking for:

Before:
    setI0100(mds3a1.getI0100().toString());
After:
    setI0100

Regex Breakdown:

\( Match and escape the ( character.

. Match any single character.

* Match preceding character 0 or more times.

Solution 2

Turn on regex search in the Find dialog, and use the following pattern:

^(.*?)(?=\()

This starts at the beginning of the line (^) then captures any character ((.*?)) up to the non-capturing positive lookahead ((?=\()).

Share:
12,095

Related videos on Youtube

Charles S
Author by

Charles S

Updated on September 18, 2022

Comments

  • Charles S
    Charles S over 1 year

    Is there a way to find and replace using a special character that would allow any char to be in it's place?

    What I mean by this is say I have the following text:

    setI0100(mds3a1.getI0100().toString());
    setI0200(mds3a1.getI0200().toString());
    setI0300(mds3a1.getI0300().toString());
    setI0400(mds3a1.getI0400().toString());
    setI0500(mds3a1.getI0500().toString());
    setI0600(mds3a1.getI0600().toString());
    setI0700(mds3a1.getI0700().toString());
    setI0800(mds3a1.getI0800().toString());
    setI0900(mds3a1.getI0900().toString());
    setI1100(mds3a1.getI1100().toString());
    setI1200(mds3a1.getI1200().toString());
    setI1300(mds3a1.getI1300().toString());
    

    and I wanted to replace using something like (mds3a1.getI***().toString()); replacing the ending of all lines above after the initial (.

    Where * could equal any character. Allowing me to replace the endings of all of the lines above even though they are not necessarily exactly all the same. One line would look like:

    Before:
        setI0100(mds3a1.getI0100().toString());
    After:
        setI0100
    

    I've seen similar functionality in other programs but I'm not exactly sure what the word for this behavior is.

    EDIT: Ended up I was looking for regular expressions as @DavidPostill pointed out below.

    To do what I wanted I ended up with the following:

    find: (\(mds3a1.getJ)....+(().toString\(\)\);)
    replace: 
    

    which would leave me with the text of each line up until and not including the first (

    For anyone looking to use regex with sublime text here is a good resource I found. https://github.com/dmikalova/sublime-cheat-sheets/blob/master/cheat-sheets/Regular%20Expressions.cheatsheet

    • MattDMo
      MattDMo over 8 years
      Do you want to match all the current options and replace them all with one thing? Please add more examples to your question, it's kind of unclear what you're asking.
    • Charles S
      Charles S over 8 years
      @MattDMo basically I want to chop off the ending, starting with "(mds" of each of these lines. I tried to clarify a bit more in the initial question.
    • Charles S
      Charles S over 8 years
      I'm trying to save myself some typing and just want to grab all text from each line before the first (
    • DavidPostill
      DavidPostill over 8 years
      That's exactly what regular expressions are for. Start reading docs.sublimetext.info/en/latest/search_and_replace/…
    • Charles S
      Charles S over 8 years
      @DavidPostill if you could make that an answer I'd be glad to give you a +
    • Charles S
      Charles S over 8 years
      I also found the following resource very helpful if anyone ends up in a similar situation. github.com/dmikalova/sublime-cheat-sheets/blob/master/…
    • MattDMo
      MattDMo over 8 years
      So you just want everything up to (but not including) the first ( character, regardless of its content?
    • Charles S
      Charles S over 8 years
      @MattDMo yes, that was the idea. I figured out by turning on regular expressions I am able to use . as a random character, so I ended up with something like find: ((mds3a1.getI)....+(().toString());) replace:
    • DavidPostill
      DavidPostill over 8 years
      @CharlesS Thanks, but I don't know Sublime regexp well enough to make a proper answer. I'm a notepad++ user ;)
  • Charles S
    Charles S over 8 years
    oooh I actually like this more than what I came up with. Thank you very much @MattDMo
  • MattDMo
    MattDMo over 8 years
    No problem. It really pays to learn regex :)
  • Charles S
    Charles S over 8 years
    my hands thank you, got 4000 some lines of this to do :>
  • MattDMo
    MattDMo over 8 years
    well, if you need more regex help, just post on Stack Overflow and tag it regex. Just make an honest attempt first, and if you mess up there are many people standing by to help. Make sure you tag it sublimetext as well, so they know which flavor they're dealing with.
  • Charles S
    Charles S over 8 years
    You are correct, In my haste I did not notice MattDMo's answer was not exactly what I wanted. I only noticed shortly after selecting it as an answer. To be fair my question was not as clear as it could have been. I also should have tested a bit more before selecting it as an answer. Thank you for providing the absolute correct answer. This was exactly what I was looking for.