2

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

9
  • 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.
    – MattDMo
    Dec 2, 2015 at 21:34
  • @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
    Dec 2, 2015 at 21:46
  • 1
    I'm trying to save myself some typing and just want to grab all text from each line before the first (
    – Charles S
    Dec 2, 2015 at 21:50
  • 1
    That's exactly what regular expressions are for. Start reading docs.sublimetext.info/en/latest/search_and_replace/…
    – DavidPostill
    Dec 2, 2015 at 21:56
  • @DavidPostill if you could make that an answer I'd be glad to give you a +
    – Charles S
    Dec 2, 2015 at 22:11

2 Answers 2

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.

1
  • 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.
    – Charles S
    Dec 3, 2015 at 21:48
1

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 ((?=\()).

4
  • oooh I actually like this more than what I came up with. Thank you very much @MattDMo
    – Charles S
    Dec 2, 2015 at 22:20
  • 1
    No problem. It really pays to learn regex :)
    – MattDMo
    Dec 2, 2015 at 22:21
  • my hands thank you, got 4000 some lines of this to do :>
    – Charles S
    Dec 2, 2015 at 22:26
  • 1
    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.
    – MattDMo
    Dec 2, 2015 at 22:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .