How can I use regex in sublime to target the end of every third line, so that I can insert a semicolon.
I know I can target/wrap every third line like this:
(.*\n){3}
And target the end of each line like this: $
But how can I target the END of every THIRD line so that I can insert a semicolon?
You shouldn't match the third newline character. Try the following regex:
^.*(?:\R.*){2}\K
See live demo here
In above regex \R means any kind of newline character, \K means reset match output and ^ matches at start of each line by default in Sublime Text (so no need for (?m)).
Put the cursor at the beginning of file content then search for the given regex and replace with ;.
Related
I have a file that has lots of lines and I have one line that starts with "1020":
990.1.1={
holder=1000083706 #Dowelani
}
1020.1.1={
holder=1000083707 #Mutsutshudzi
}
1050.1.1={
holder=1000083708 #Khathu
}
I want to remove every line above that line starting with 1020, but I want to keep the 1020 line.
I have been trying .*1020, and this removes everything before the line containing "1020", but it also removes the 1020. How can I modify the code to keep the line I search for but also remove every line above it?
Rather that discarding the part of the string up to the target string it's easier to simply match the line that begins with the target string and all subsequent lines. You can do that with the regular expression
^1020\..*
with the multiline and single line (or dotall) flags set.
Demo
The multiline flag causes ^ to the match the beginning of a line, rather than the beginning of the string, and the single line flag causes . to match every character, including line terminators. (Without that flag set . matches all characters other than line terminators.)
If you only want to keep the (first) line that begins with the target string, do not set the single-line flag and return the first match (using re.search()).
You can use
Find What: (?s)^.*?\R(?=1020\.)
Replace With: empty string
See the regex demo. Details:
(?s) - a dot now matches newlines, too
^ - start of a line
.*? - any zero or more chars, as few as possible
\R - a line break sequence
(?=1020\.) - a positive lookahead that matches a location in string that is immediately followed with 1020..
Say I have the following (where <space> indicates an actual whitespace character)
Line 1
Line 2<space>
Line 3
Line 4<space>
Line 5
and I wanted it to be
Line 1/
Line 2/
Line 3/
Line 4/
Line 5/
I initially thought that maybe s/\s*$/\//g would work but then I ended up with the following as the solution
Line 1/
Line 2//
Line 3/
Line 4//
Line 5/
I can't think of a way to include the case where there is a whitespace character at the end that I want replaced.
\s*$ can match a string ending in a space twice; once to capture the actual space character, and again to match the end of string (see for example this Q&A). This is why you are getting two replacements on the strings ending in space. To avoid this problem, use a positive lookbehind for a non-space character; that prevents the end of string that is preceded by a space being matched again. In normal regex you would match with:
(?<=\S)\s*$
and replace with a /. Demo on regex101.
In vim you can use the \zs marker to define the start of the match e.g.
:%s/\S\zs\s*$/\//
I have 50 files which have a blank first line and column headers surrounded in double quotes on the second line. I want to delete the first line and remove double quotes " from the second line for every file.
Can both these changes be done in 1 regular expression or do I need to use two different expressions?
Note: I am unable to print the first line as blank in sample data as this website is not allowing me. The \n is just to denote an empty line.
Also the second line is different in all 50 file, so I cannot use simple find and replace. I need to use some regular expression.
Sample data.
\n
"PRODUCTID","ATTRIBUTENAME_VALUE","STATE"
"00300678116042","NOT_APPLICABLE","CONFIRMED"
"00041260363603","NOT_APPLICABLE","CONFIRMED"
Expected output
PRODUCTID,ATTRIBUTENAME_VALUE,STATE
"00300678116042","NOT_APPLICABLE","CONFIRMED"
"00041260363603","NOT_APPLICABLE","CONFIRMED"
I think this should work as one replace find in files:
Find what: ^\r\n"(.*?)","(.*?)","(.*?)"
Replace with: \1,\2,\3
You can try something like this:
(?:\G(?!^)|^\R)"([^"\n]*)
and replace it with $1.
pattern details:
(?:
\G # contiguous to the previous match
(?!^) # not at the start of the line
# (to prevent \G to match the start of the string)
| # OR
^\R # start of a line followed by a newline (an empty line)
)
"
([^"\n]*) # capture group 1: all that is not a quote or a newline
# (to reach the next quote)
I'm very new to regex, what I'm trying to do is to match a line only if the next line is an empty line.
For example:
This is some text
( empty line )
This is some text
This is some text
This is some text
( empty line )
This is some text
( empty line )
In the previous example, I would like to be able to select only line 1,5,7.
Is this possible with regex in notepad++?
Thanks in advance.
You can use this regex,
(.*)\n\s*\n
and replace with
\1
Working Demo
It uses a concept of group capture, so here you can use \1 to use captured group, that is line before newline
You could try the below positive lookahead based regex.
^.*?\S.*(?=\n[ \t]*$)
\S matches any non-space character. So .*?\S.* matches the line which has at-least one non-space character and the following (?=\n[ \t]*$) asserts that the match must be followed by a newline character and then followed by zero or more space or tab characters.
OR
^.*?\S.*(?=\n\n)
If you mean empty line as line which don't have any single space or tab characters, then you could use the above regex. (?=\n\n) asserts that the match must be followed by a blank line.
DEMO 1
DEMO 2
This should do the trick:
/(.)*\n\n/
If you're looking for an easy way to test / verify regex rubular is pretty good.
If i have a line of text that i want to remove from a text file in notepad and it is always formatted like this
[text]:
except that the words in the text area change. what is a regular expression i could create to remove the whole section with the search and replace function in notepad?
To delete the entire line starting with [any text]: you can use: ^[\t ]*\[.*?\]:.*?\r\n
Explanation:
^ ... start search at beginning of a line (in this case).
[\t ]* ... find 0 or more tabs or spaces.
\[ ... find the opening square bracket as literal character.
.*? ... find 0 or more characters except the new line characters carriage return and line-feed non greedy which means as less characters as possible to get a positive match, i.e. stop matching on first occurrence of following ] in the search expression.
\]: ... find the closing square bracket as literal character and a colon.
.*?\r\n ... find 0 or more characters except the new line characters and finally also the carriage return and line-feed terminating the line.
The search string ^[\t ]*\[.*?\]:.*?$ would find also the complete line, but without matching also the line termination.
The replace string is for both search strings an empty string.
If by removing the entire section, you mean remove the [text]: up to the next [otherText]:, you can try this:
\[text\]:((?!\[[^\]]*\]:).)*
Remember to set the flag for ". matches newline".
This regex basically first matches your section title. Then, it would start matching right after this title and for each character, it uses a negative lookahead to check if the string following this character looks like a section title. If it does the matching is terminated.
Note: Remember that this regex would replace all occurrences of the matched pattern. In other words, if you have more than one of that section, they are both replaced.