I am trying to mass comment out all the lines referring to logging in a code.
I am using notepad ++ and ideally this would be achievable by replacing all the lines being like
< some text > logging
by
# < some text > logging
Sorry in advance to ask this kind of question on SO, but I'm kind of stuck , and I'm sure this can be useful to other people.
In notepad++,
Search "^(.*) logging"
Replace "#\1 logging"
Make sure search mode is "Regular expression"
Find with (?=.*logging)^ and replace with #
Eplanation:
(?=.*logging) will lookahead for word logging and if present search for beginning of string with ^
Regex101 Demo
Find what : ^.*logging$
Replace with: # $0
Search mode: Regular expression
$0 is a variable for the match.
And the regex matches a line that ends with "logging".
So replacing it with # $0 puts a # at the start of a line that ends with logging.
^ : start of a line
.* : zero or more characters
$ : end of a line
And to a avoid commenting lines that are already are commented?
Adding a negative lookahead helps with that:
Find what: ^(?!\s*#).*logging$
Related
I am trying to use Regex in notepad++ to select everything after v+(number|character)* but in the selection it should excluded the v+(num|char)*.
e.g. master\_\move_consolidate_archives_html_to_move_base_v2kjkj_(2021_01_19_11h43m59s-fi_m_dt xx-) - Copy (2).bat"
I am expecting
_(2021_01_19_11h43m59s-fi_m_dt xx-) - Copy (2).bat"
so far I can use this line (?i)(v\d[0-9a-z]*)
to select v2kjkj
but I can't get this to work with lookbehind (?<=xxxx).
I am also trying to use if-then-else condition but no luck for me. I am still don't understand enough to using it.
issue.
because the "v" have different pattern in it. I can't hard code to certain string
v2
v23
v2kjkj
v2343434
Test string:
mmaster\_\move_consolidate_archives_html_to_move_base_v2_16_.bat"
master\_\move_consolidate_archiv es_html_to_move_base_v23_17_.bat"
master\_\move_consolidate_archives_html_to_move_base_v2_17_(2021_01_19_12h37m19s-fi_m_dt xx-).bat"
master\_\move_consolidate_archives_html_to_move_base_v2_(2021_01_19_11h43m59s-fi_m_dt xx-) - CopyCopy.bat"
master\_\move_consolidate_archives_html_to_move_base_v2kjkj_(2021_01_19_11h43m59s-fi_m_dt xx-) - Copy (2).bat"
master\_\move_consolidate_archives_html_to_move_base_v2343434_(2021_01_19_11h43m59s-fi_m_dt xx-) - Copy (3).bat"
I have been reading and searching for a day but I can't apply anything I have seen so for.
the closest one I see was
Regexp match everything after a word
Getting the text that follows after the regex match
I am welcome any comments.
Ctrl+H
Find what: v\d[0-9a-z]*\K.*$
Replace with: LEAVE EMPTY
UNCHECK Match case
CHECK Wrap around
CHECK Regular expression
UNCHECK . matches newline
Replace all
Explanation:
v # a "v"
\d # a digit
[0-9a-z]* # 0 or more alphanum
\K # forget all we have seen until this position
.* # 0 or more any character but newline
$ # end of line
Screenshot (before):
Screenshot (after):
I have something like this in txt
[[asdfg]] [[abcd|qwerty]]
in a row, but I want it to look like that
[[asdfg]] [[qwerty]]
using wildcards ( [[.*\| ) when trying to search, results in it finding the whole line up to the "|" Not allowing it to have a space in between should work, but I don't know how to do that.
Edit 1
It's from a wikipedia dump, so the first part is the word in it's basic form and the second is how it fits into the sentence. Something like [[I]] [[be|was]] [[at]] [[the]] [[doctor]] And I want to change it into normal sentences
[[I]] [[was]] [[at]] [[the]] [[doctor]]
Edit 2
I found somewhat of a solution. I just put every word in a new line, did the first regex and then deleted newlines. That did kinda mess up my spacing though...
Try this regex:
\[\[\w+\|(\w+)\]\]
Replace with:
[[$1]]
Make sure you choose Regular expression at the bottom before you click Replace All in Notepad++.
You can do it all in one run like so
\[{2}(?:(?!\]{2}).)+?\|([^\]]+)
This needs to be replaced by
[[$1
See a demo on regex101.com.
Broken down this says:
\[{2} # match [[
(?:(?!\]{2}).)+? # do not overrun ]]
\| # |
([^\]]+) # capture anything not ] into group 1
Afterwards, you'll only need to replace the open brackets and the content of group $1
I am trying to extract a report of all incidents matching a certain pattern and then need to plot how many occurances of each type. For example the below lines.
File: ../../../transfer/200.FILETYPE1.0000003115.20160419-082708-089.xml successfully imported.
some other logs....
File: ../../../transfer/200.FILETYPE1.0000003116.20160419-082708-090.xml successfully imported.
some other logs...
File: ../../../transfer/201.FILETYPE2.0000003117.20160419-082708-091.xml successfully imported.
Please note that there are many filetypes but the pattern is same "/transfer/" prefix and "successfully imported." suffix and these prefix and suffix must match as other lines may also contain same file name before completion.
So in above case I need to find all such occurrences of above lines and find count of each FILETYPE1 and FILETYPE2 in splunk.
Can someone help me with regex that can match above pattern and give me all such lines so that I can extract counts of each file type?
Straight forward:
^File:.*FILETYPE\d.*$
# ^ beginning of the line
# File: literally
# .* anything to the end of the line
# FILETYPE + a number literally
# .* anything afterwards
# $ the end of the line
See a demo on regex101.com.
Hint: If you only have these two strings (FILETYPE1 and FILETYPE2) you might be faster with string functions only.
Edit FILETYPE1/FILETYPE2 for counting
\.\.\/.*\/\d+\.FILETYPE1\..*?\.xml
Regex demo
Try this one:
\/transfer\/.*FILETYPE(\d+).*successfully imported
The file type number will be captured by the capture group, so you can count the file occurrences
Regex Demo
I'm trying to use regex to find single quotes (so I can turn them all into double quotes) anywhere in a line that starts with mySqlQueryToArray (a function that makes a query to a SQL DB). I'm doing the regex in Sublime Text 3 which I'm pretty sure uses Perl Regex. I would like to have my regex match with every single quote in a line so for example I might have the line:
mySqlQueryToArray($con, "SELECT * FROM Template WHERE Name='$name'");
I want the regex to match in that line both of the quotes around $name but no other characters in that line. I've been trying to use (?<=mySqlQueryToArray.*)' but it tells me that the look behind assertion is invalid. I also tried (?<=mySqlQueryToArray)(?<=.*)' but that's also invalid. Can someone guide me to a regex that will accomplish what I need?
To find any number of single quotes in a line starting with your keyword you can use the \G anchor ("end of last match") by replacing:
(^\h*mySqlQueryToArray|(?!^)\G)([^\n\r']*)'
With \1\2<replacement>: see demo here.
Explanation
( ^\h*mySqlQueryToArray # beginning of line: check the keyword is here
| (?!^)\G ) # if not at the BOL, check we did match sth on this line
( [^\n\r']* ) ' # capture everything until the next single quote
The general idea is to match everything until the next single quote with ([^\n\r']*)' in order to replace it with \2<replacement>, but do so only if this everything is:
right after the beginning keyword (^mySqlQueryToArray), or
after the end of the last match ((?!^)\G): in that case we know we have the keyword and are on a relevant line.
\h* accounts for any started indent, as suggested by Xælias (\h being shortcut for any kind of horizontal whitespace).
https://stackoverflow.com/a/25331428/3933728 is a better answer.
I'm not good enough with RegEx nor ST to do this in one step. But I can do it in two:
1/ Search for all mySqlQueryToArray strings
Open the search panel: ⌘F or Find->Find...
Make sure you have the Regex (.* ) button selected (bottom left) and the wrap selector (all other should be off)
Search for: ^\s*mySqlQueryToArray.*$
^ beginning of line
\s* any indentation
mySqlQueryToArray your call
.* whatever is behind
$ end of line
Click on Find All
This will select every occurrence of what you want to modify.
2/ Enter the replace mode
⌥⌘F or Find->Replace...
This time, make sure that wrap, Regex AND In selection are active .
Them search for '([^']*)' and replace with "\1".
' are your single quotes
(...) si the capturing block, referenced by \1 in the replace field
[^']* is for any character that is not a single quote, repeated
Then hit Replace All
I know this is a little more complex that the other answer, but this one tackles cases where your line would contain several single-quoted string. Like this:
mySqlQueryToArray($con, "SELECT * FROM Template WHERE Name='$name' and Value='1234'");
If this is too much, I guess something like find: (?<=mySqlQueryToArray)(.*?)'([^']*)'(.*?) and replace it with \1"\2"\3 will be enough.
You can use a regex like this:
(mySqlQueryToArray.*?)'(.*?)'(.*)
Working demo
Check the substitution section.
You can use \K, see this regex:
mySqlQueryToArray[^']*\K'(.*?)'
Here is a regex demo.
I have a file with 1000's of rows looking like
"20140611","20:19","C","IT","IT","HDR","HDPDIT","675605","000000135.97"," ..........
I am trying to replace all occurrences of string that matches this pattern :
quote then 6 numerics followed by a closing quote ( i.e. "675605" with "675605#")
Using edit plus regular expression search and replace, the search string is :
\"[0-9][0-9][0-9][0-9][0-9][0-9]\"
This will find all the occurrences I need
However I'm unable construct the correct replace with reg ex to replace the match with itself followed by the # sign e.g. "675605#
With sed you can have:
sed -r 's|"([0-9]{6})"|"\1#"|g' file
Add -i to modify it inline.
So my proposed regex - replacement form is:
"([0-9]{6})" - "\1#"
Quoted:
\"([0-9]{6})\" - \"\\1#\"
Regex:
\"([0-9][0-9][0-9][0-9][0-9][0-9])\"
Replacement string:
"\1#
DEMO
Replacement string would be "\1" if you want "675605#"
You need to use capturing groups. I don't know if you can use them in Edit Plus, but I think it should work:
Find what: \"(\d{6})\"
Replace with: \"\1#\"
Where \1 is a number captured in parenthesis.
Open your file in vim or vi editor using below command:
vi "filename"
then use this command it replace your "675605" pattern with "675605#"
:%s/675605/675605#/g
then
esc :wq
now you open your file it replaced all your "675605" pattern with "675605#".