How to add contents to the end of a line

Answered

I want to add a semi-colon to the end of a few hundred lines. I can't figure out how to do this with the Search/Replace in IntelliJ's editor (I have 4.5) since it only recognizes pure text, not end of line characters. Is there a way to do this?

4 comments
Comment actions Permalink

Simple ;)

in the Replace dialog find the checkbox which says "Regular
Expressions". For IDEA it means that the search pattern and replace
pattern are not simple symbols, but regexp which is to be processed as
such. The regexp for "new line" is \n. So in the Text To Find box type:

\n

(with words - slash, en)

The regexp for semicolon followed by new line is:

;\n

so type this in the "Replace with" box.
Scope: global;
Direction: forward;

do the "search and replace" now.


Other way, similar result:

in the Find box type $ (the dollar sign). In regexp it means "the
position just before the end of the line". In the Replace box type ;
(semicolon). In regexp it means "semicolon" :)). The all together means
for IDEA: place semicolon just before each line-terminator (namely, as
last symbol of the line)

More about regexp in Java here:

http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html

also google ;)

Please report if it helped or not, so the thread becomes useful for
other people with a similar question.


Greetings!
George

Suzanne Dorman wrote:

I want to add a semi-colon to the end of a few hundred lines. I can't figure out how to do this with the Search/Replace in IntelliJ's editor (I have 4.5) since it only recognizes pure text, not end of line characters. Is there a way to do this?

0
Comment actions Permalink

Thank you George, it worked great. For some reason, I never noticed the Regular Expression check box on the Replace dialog before. Time to get my eyes checked! Thanks again!

0
Comment actions Permalink

I have almost this exact same question, except I only want to add the semi colon to lines that need it, eg css rules that dont already have one.. so i have this regex:

[^{};,]$

But I am not sure how to *add* a character, this regex just selects the last character in the line where I need to inject the semi colon after it. But i cannot figure out how to add it in. I've tried $1; but it just says <empty string>

0
Comment actions Permalink

@Hendris3, you want to surround that text with parenthesis.

find: ([^{};,]$)  // this is capturing it so you can use it in the replace box.

replace: $1;

0

Please sign in to leave a comment.