Remove Multiple Periods Up To Bracket From String - regex

Would like to know how to create an Emacs macro that will
Find the first instance of multiple periods in string
Set mark
Move to the first closed bracket in string
Remove all chars between mark and closed bracket
Here is an example string. I'd like to go from this:
* [This is Chapter 1.......................................................... 1-83](chapter1.md)
To this:
* [This is Chapter 1](chapter1.md)
Can anyone assist?
Thanks

Heres the hacky way I accomplished. I'm sure there is a cleaner way.
Start with cursor at the beg of line
M-x start-kbd-macro
C-s RET .. to search for first instance of ".." in the string
C-SPACE to set mark
C-s ] to search for first instance of "]" in the string
DEL to remove everything marked
BKSP BKSP to remove the final two ".."
DWN ARROW to get to next line
C-a to get to beg of line
M-x end-kbd-macro
I know its lame, but it worked!! I have ~100 pages of docs to do this to! Need to figure out how to reliably perform this on the entire doc next.

Related

Tcl - How to Add Text after last character through regex?

I need a tip, tip or suggestion followed by some example of how I can add an extension in .txt format after the last character of a variable's output line.
For example:
set txt " ONLINE ENGLISH COURSE - LESSON 5 "
set result [concat "$txt" .txt]
Print:
Note that there is space in the start, means and fin of the variable phrase (txt). What must be maintained are the spaces of the start and means. But replace the last space after the end of the sentence, with the format of the extension [.txt].
With the built-in concat method of Tcl, it does not achieve the desired effect.
The expected result was something like this:
ONLINE ENGLISH COURSE - LESSON 5.txt
I know I could remove spaces with string map but I don't know how to remove just the last occurrence on the line.
And otherwise I don’t know how to remove the last space to add the text [.txt]
If anyone can point me to one or more solutions, thank you in advance.
set result "[string trimright $txt].txt"
or
set result [regsub {\s*$} $txt ".txt"]

Replace end of string when start matched

This is what I have in first file:
16;01978B66;BC101;FALSE
17;0195B4E5;BC101;FALSE
18;019796C6;BC101;FALSE
19;0197D016;BC101;FALSE
This is what I have in 2nd file
16;01978B66;BC102;FALSE
17;0195B4E5;BC102;FALSE
18;019796C6;BC102;FALSE
19;0197D016;BC102;FALSE
What regex should I use if I want to replace end of every line starting with 16; and 18; , from ;FALSE to ;TRUE ? I would like to use notepad++ replace in files, so I can replace multiple lines 16; and 18; in all files without touching middle of the string with different values.
I understand regex once I get it explained but I searched for hours and I get lost in other examples...
This is what I should get:
16;01978B66;BC101;TRUE
17;0195B4E5;BC101;FALSE
18;019796C6;BC101;TRUE
19;0197D016;BC101;FALSE
and
16;01978B66;BC102;TRUE
17;0195B4E5;BC102;FALSE
18;019796C6;BC102;TRUE
19;0197D016;BC102;FALSE
I tried to capture in 3 groups with
^(17;)[a-zA-Z0-9\;]{9}[a-zA-Z0-9\;]{6}[a-zA-Z0-9\;]{5}
but replace with ($3);TRUEis leaving me with only;TRUE` which is not good.
This must be piece of cake for someone who knows how to replace end of string.
btw 0197D016;BC101; is constant in lenght, 8 digits ; 2letters3numbers ;
Thanks in advance for help.
Please try the following:
Find what: ^((16|18);.+?)FALSE$
Replace with: $1TRUE

find a pattern in string and remove that pattern of the string from excel cells without touching the pattern in the middle of the string

I have a column which has "--" pattern in the beginning, middle and end of the string. For example:
-- myString
my -- String
myString --
I want to find these two types of cells
-- myString
myString --
and remove the "--" pattern, so it will look fine! I am an amateur user of excel but can use functions if you suggest me. It should be possible with find and use the results of the Find in Replace functions, but I do not know how to pass the results to Replace.
Please note: The answer should take care all the cells in the column, which are hundreds. One solution for changing all, not one solution for one cell.
EDIT: Just reread the request, per instruction from Gary'sStudent. This will remove all instances of "--", not only those at the beginning/end.
If the data is in A1, use the following formula:
=SUBSTITUTE(A1,"--","")
With data in A1 in B1 enter:
=IF(LEFT(A1,2)="--",MID(A1,3,9999),IF(RIGHT(A1,2)="--",MID(A1,1,LEN(A1)-2),A1))
OK, I found the answer. The answer from #Dubison helped me to find the right answer.
If the left two characters in this cell is "--" and the last two characters are "--" the substitute the "--" with "", else to nothing.
=IF(LEFT(A1,2)="--",SUBSTITUTE(A1,"--",""),IF(RIGHT(A1,2)="--",SUBSTITUTE(A1,"--",""), A1))
This will be pretty much the same with previous answers, only using simpler logic. If your strings first or last character = "-" do nothing, else replace "--" with "".
=IF(LEFT(A1,1)="-",A1,IF(RIGHT(A1,1)="-",A1, SUBSTITUTE(A1,"--","")))
UPDATE:
I noticed that I have misread the question. Above code will remove the "--" only if it is in the middle. However original question was to remove "--" only if it is at the beginning or at the end. So formula should be:
=IF(OR(LEFT(A1,2)="--",RIGHT(A1,2)="--"),SUBSTITUTE(A1,"--",""),A1)

add character before first word in line

I want to add a minus sign "-" infront of the first word in a line on the editor VIM. The lines contains spaces for indentation. The indentation shall not be touched. E.g
As Is
list point 1
sub list point 2
and so on...
I want
- list point 1
- sub list point 2
- and so on...
I can find the first word, but i struggle with replacing it in the correct way.
^\s*\w
in Vim
/^\s*\w
But in the replacement I always remove the complete found part....
:s/^\s*\w/- \w/
Which leads to
- ist point 1
- ub list point 2
- nd so on...
Use & which is replaced with the matched string:
:%s/\w/- &
I'm late to the party but:
:%norm! I- <CR>
And another one with :s:
:%s/^\s*/&- /
An alternative to falsetrue's answer: You can capture the first word character and print it out along with the leading -:
%s/\(\w\)/- \1/
:normal cmd may help too:
:%norm! wi-
note that after - there is a space.

Remove the first character of each line and append using Vim

I have a data file as follows.
1,14.23,1.71,2.43,15.6,127,2.8,3.06,.28,2.29,5.64,1.04,3.92,1065
1,13.2,1.78,2.14,11.2,100,2.65,2.76,.26,1.28,4.38,1.05,3.4,1050
1,13.16,2.36,2.67,18.6,101,2.8,3.24,.3,2.81,5.68,1.03,3.17,1185
1,14.37,1.95,2.5,16.8,113,3.85,3.49,.24,2.18,7.8,.86,3.45,1480
1,13.24,2.59,2.87,21,118,2.8,2.69,.39,1.82,4.32,1.04,2.93,735
Using vim, I want to reomve the 1's from each of the lines and append them to the end. The resultant file would look like this:
14.23,1.71,2.43,15.6,127,2.8,3.06,.28,2.29,5.64,1.04,3.92,1065,1
13.2,1.78,2.14,11.2,100,2.65,2.76,.26,1.28,4.38,1.05,3.4,1050,1
13.16,2.36,2.67,18.6,101,2.8,3.24,.3,2.81,5.68,1.03,3.17,1185,1
14.37,1.95,2.5,16.8,113,3.85,3.49,.24,2.18,7.8,.86,3.45,1480,1
13.24,2.59,2.87,21,118,2.8,2.69,.39,1.82,4.32,1.04,2.93,735,1
I was looking for an elegant way to do this.
Actually I tried it like
:%s/$/,/g
And then
:%s/$/^./g
But I could not make it to work.
EDIT : Well, actually I made one mistake in my question. In the data-file, the first character is not always 1, they are mixture of 1, 2 and 3. So, from all the answers from this questions, I came up with the solution --
:%s/^\([1-3]\),\(.*\)/\2,\1/g
and it is working now.
A regular expression that doesn't care which number, its digits, or separator you've used. That is, this would work for lines that have both 1 as their first number, or 114:
:%s/\([0-9]*\)\(.\)\(.*\)/\3\2\1/
Explanation:
:%s// - Substitute every line (%)
\(<something>\) - Extract and store to \n
[0-9]* - A number 0 or more times
. - Every char, in this case,
.* - Every char 0 or more times
\3\2\1 - Replace what is captured with \(\)
So: Cut up 1 , <the rest> to \1, \2 and \3 respectively, and reorder them.
This
:%s/^1,//
:%s/$/,1/
could be somewhat simpler to understand.
:%s/^1,\(.*\)/\1,1/
This will do the replacement on each line in the file. The \1 replaces everything captured by the (.*)
:%s/1,\(.*$\)/\1,1/gc
.........................
You could also solve this one using a macro. First, think about how to delete the 1, from the start of a line and append it to the end:
0 go the the start of the line
df, delete everything to and including the first ,
A,<ESC> append a comma to the end of the line
p paste the thing you deleted with df,
x delete the trailing comma
So, to sum it up, the following will convert a single line:
0df,A,<ESC>px
Now if you'd like to apply this set of modifications to all the lines, you will first need to record them:
qj start recording into the 'j' register
0df,A,<ESC>px convert a single line
j go to the next line
q stop recording
Finally, you can execute the macro anytime you want using #j, or convert your entire file with 99#j (using a higher number than 99 if you have more than 99 lines).
Here's the complete version:
qj0df,A,<ESC>pxjq99#j
This one might be easier to understand than the other solutions if you're not used to regular expressions!