So basically i have text in format something:something:something
and i want to remove the first "something"+colon on every new line.
Try with this:
Search: ^[^:]*:([^\n]*)
Replace by: \1
Select: "Regular expressions"
Replace All
Related
So I am trying to remove the rest of the line after specific string on a document but I am finding it hard.
The lines look similar to this
abc:abc|bca:111 222|ccc:01/01/17
abc:abc|bca:bca bca|ccc:02/02/17
I am trying to delete everything after abc:abc including | but I don't know how.
I tried it with this on notepad+ \|.bca* but when I click Replace all it selects whole document.
Ctrl+H
Find what: \|.*$
Replace with: LEAVE EMPTY
check Wrap around
check Regular expression
DO NOT CHECK . matches newline
Replace all
Explanation:
\| : pipe character
.* : 0 or more any character but newline
$ : end of line
Result for given example:
abc:abc
abc:abc
I have about 256 lines in a text file that look like /*0*/L"", I want to remove the last , and then put the remaining as a function argument code.append(/*0*/L""); I tried doing it with vim but I don't have much experience in it. how can we place something within something else in vi or vim?
:%s#\v(/\*0\*/L""),#code.append(\1);#
:%s : substitute all lines
# : alternative separator
\v : use very magic mode (see :h magic)
(/\*0\*/L""), : capture the regex, excluding the trailing comma
\1 : insert first captured group
this line would do the substitution on all lines in your buffer, only if the line ending with comma. No matter you had /*0*/L"", or /*123*/L"",
%s/\v(.*),$/code.append(\1)/
if you want to shrink the sub on certain pattern, change the .* part in the above cmd to fit your needs.
I have the following problem
This is text:
printf("sysname %s",ut.sysname);
I want to use vim to replace sysname line by line. I type the command in my gvim:
:s/sysname/version
I want to get the output like this:
printf("version %s",ut.version);
But I get the output like this:
printf("version %s",ut.sysname);
What am I doing wrong?
you're missing the g command that applies to all matches on current line, instead of only the first one:
:s/sysname/version/g
as a bonus:
:%s/sysname/version/g
will replace all occurences in current file, not only on the current line.
To do it on one line
:s/sysname/version/g
You can also use the qq macro recorder before typing that in, and press q after, and then use #q to replay that on any other lines you want to replace that on. Or press : up to select old commands.
Or to do it on every single line:
:%s/sysname/version/g
However with replacing every line you should be careful. If there is a lot of text try making your replacements more specific.
I would do
:%s/\(printf("\)sysname\(.*\)sysname/\1version\2version
Doing some String manipulation and I want to ask if the below is possible in Notepad++:
I have a string with Years:
10-Jan-13
22-Feb-14
10-Jan-13
10-Mar-13
I want
10-JAN-13
22-FEB-14
10-JAN-13
10-MAR-13
(There's more data on each line there but I am just showing a simplified example).
I know I can OR search with | character so find, JAN|FEB|MAR... but how do I replace according to what's found.
(Just trying to save some time)
Thanks.
Not sure if it's a plugin or built-in, but you can use the TextFX Characters plugin, to select the text, and then in the textfx characters dropdown, click UPPER CASE.
Update
Looks like it is a plugin:
TextFX menu is missing in Notepad++
Multiple Files
I found this site which gives a way to convert text to uppercase with regular expressions: http://vim.wikia.com/wiki/Changing_case_with_regular_expressions
So, what you can do is bring up the find in files dialog (CTRL+SHIFT+F), change search mode to Regular Expression, and then use something like this:
Find: (\d{2}-\w{3}-\d{2})
Replace with: \U\1
Directory: Whichever directory your files are in (and only the files you want changed).
\U is an uppercase flag, and the brackets in the Find regex correspond with the \1 backreference, which will basically replace it with itself (but uppercase).
I always wanted to know, how you can substitute within given parameters.
If you have a line like this:
123,Hello,World,(I am, here), unknown
and you wnat to replace World with Foobar then this is an easy task: :%s/World/Foobar/
Now I wonder how I can get rid of a , which is wihtin the ( ).
Theoretically I just have to find the first occurance of ( then substitute the , with a blank until ).
Try lookahead and lookbehind assertions:
%s/([^)]*\zs,\ze.*)//
(\zs and \ze tell where pattern starts and end)
or
%s/\(([^)]*\)\#<=,\(.*)\)\#=//
The first one is more readable, the second one uses \( ... \) groupings with parentheses inside groups which makes it look like obfuscated, and \#<= which apart from being a nice ASCII-art duck is the lookbehind operator, and \#= that is the lookahead operator.
References: :help pattern (more detail at :help /\#=, :help /\ze, etc.)
You use the GUI and want to try those commands? Copy them into the clipboard and run :#+ inside Gvim.
Modifying slightly the answer of #Tom can give you a quite good and "a bit" more readable result :
%s/\(.*\)(\(.*\),\(.*\))\(.*\)/\1(\2\3)\4/
That way you will have : in \1 will store what is at the left outside of the parenthesis, \4 what is at the right outside of the parenthesis and \2 and \3 what is inside the parenthesis, respectively on the left (\2) and on the right (\3).
With that you can easily swap your elements if your file is organised as column.
You can also select the text you want to change (either with visual or visual-block modes) and enter the : to start the replace command. vi will automatically start the command with :'<,'> which applies the command to the selected area.
Replacing a , can be done with:
:'<,'>s/,/ /g
For your example, this is the same thing as suggested by #ubuntuguy
%s/\(.*\)(\(.*\),\(.*\)/\1(\2\3
This will do the exact replacement you want.
Yet another approach, based on the fact that actually you want to substitute only the first occurrence of , inside the parenthesis:
:%s#\((.\{-}\),#\1 #
Explanation:
:%s for substitution in the whole file (don't use % if you want to work only with the current line)
we can use # or : as a delimiter to make the command more readable
in (.\{-} we ask to find any symbol (dot) after the left parenthesis and the rest stands for 0 or more occurrence (as few as possible) of this symbol. This expression is put inside \(...\) to be able to refer to this group as \1 in the future.