I want to delete all lines ending with |
I tried
.*[|;]
but it's not the end
Use the following regex:
.*\|$
This says "any character any number of times (.*), followed by a pipe (\| - you have to escape it), and then the end of a line ($)".
If you want to find lines ending with either ; or |, use:
.*[\|;]$
You don't have to escape the pipe in this case, but I prefer to do so anyway.
In either case, make sure you're in "Regular expression" search mode with ". matches newline" unchecked.
Related
I am new to Regex world. I would like to rename the files that have time stamp added on the end of the file name. Basically remove last 25 characters before the extension.
Examples of file names to rename:
IMG523314(2021-12-05-14-51-25_UTC).jpg > IMG523314.jpg
Test run1(2021-08-05-11-32-18_UTC).txt > Test run1.txt
To remove 25 characters before the .extension (2021-12-05-14-51-25_UTC)
or if you like, remove the brackets ( ) which are always there and everything inside the brackets.
After the right bracket is always a dot '. "
Will Regex syntax as shown in the Tittle here, select the above? If yes, I wonder how it actually works?
Many Thanks,
Dan
Yes \(.*\) will select the paranthesis and anything inside of them.
Assuming when you ask how it works you mean why do the symbols work how they do, heres a breakdown:
\( & \): Paranthesis are special characters in regex, they signify groups, so in order to match them properly, you need to escape them with backslashes.
.: Periods are wildcard matcher, meaning they match any single character.
*: Asterisks are a quantifier, meaning match zero to inifite number of the previous matcher.
So to put everything together you have:
Match exactly one opening parathesis
Match an unlimited number of any character
Match exactly one closing bracket
Because of that closing bracket requirement, you put a limit to the infinite matching of the asterisk and therefore only grab the parenthesis and characters inside of them.
Yes, it's possible:
a='IMG523314(2021-12-05-14-51-25_UTC).jpg'
echo "${a/\(*\)/}"
and
b='Test run1(2021-08-05-11-32-18_UTC).txt'
echo "${b/\(*\)/}"
Explanation:
the first item is the variable
the second is the content to be replaced \(*\), that is, anything inside paranthesis
the third is the string we intend to replace the former with (it's empty string in this case)
Im using Notepad++ Find and replace and I have regex that looks for [^|]\r which will find the end of the line that starts with 8778.
8778|44523|0||TENNESSEE|ADMINISTRATION||ROLL 169 BATCH 8|1947-09-22|0|OnBase
See Also 15990TT|
I want to basically merge that line with the one below it, so it becomes this:
8778|44523|0||TENNESSEE|ADMINISTRATION||ROLL 169 BATCH 8|1947-09-22|0|OnBase See Also 15990TT|
Ive tried the replace being a blank space, but its grabbing the last character on that line (an e in this case) and replacing that with a space, so its making it
8778|44523|0||TENNESSEE|ADMINISTRATION||ROLL 169 BATCH 8|1947-09-22|0|OnBas
See Also 15990TT|
Is there any way to make it essentially merge the two lines?
\r only matches a carriage return symbol, to match a line break, you need \R that matches any line break sequence.
To keep a part of a pattern after replacement, capture that part with parentheses, and then use a backreference to that group.
So you may use
([^|\r])\R
Replace with $1. Or with $1 if you need to append a space.
Details
([^|\r]) - Capturing group 1 ($1 is the backreference that refers to the group value from the replacement pattern): any char other than | and CR
\R - any line break char sequence, LF, CR or CRLF.
See the regex demo and the Notepad++ demo with settings:
The issue is you're using [^|] to match anything that's not a pipe character before the carriage return, which, on replacement, will remove that character (hence why you're losing an e).
If it's imperative that you match only carriage returns that follow non-pipe characters, capture the preceding character ([^|])\r$ and then put it back in the replacement using $1.
You're also missing a \n in your regex, which is why the replacement isn't concatenating the two lines. So your search should be ([^|])\r\n$ and your replace should be $1.
Find
(\r\n)+
For "Replace" - don't put anything in (not even a space)
I'm not sure whether I couldn't find the correct way or this is a bug.
I wanted to check some reference manual but there doesn't seem to be one.
In Jupyter's Find and Replace screen, there's an icon .* to check when I want to use regex.
Mostly it works fine, but if I try to match a line break (\n), it does not match it unless it is that very character. For example, I want to match every line that doesn't end with , and join that line to the next one. I'd match [^,]\n and replace with ,, which would remove the line break. I could try [^,]$, but replacing this wouldn't remove the line break.
How do I do this?
There are a lot of variants of the new-line character.
E.g.: \r or \n
Regex Pattern
Anyway, here is the pattern using lookahead to check if there is a comma before, and the variants of new-line character.
(?<!,)(\r?(\n|\r))
Regex Demo
I am having hundreds of lines as illustrated below, with more than one opening double-quote (“) occurring within almost every line as shown below:
... “ ... “ ..... “ .....
note: those dots (...) above denote both words & spaces in this context for illustrative purposes.
How to search (via regex) for every such occurrence within every line? I tried achieving this with:
“.*“ or,
“.* “
but it is disappointingly returning even those who are proper i.e., with both opening & closing double quotes also (which is the correct way it should be) as follows:
... “ ...” ..... “ .....” ...... “ .....
For every second [space]“ recurring within every line it encounters — How to replace them (via regex) into ” [space]?
use [^”]* instead of .*, so it will search all occurence of two opening quotes with any character sequence in between except of closing qoute.
EDIT:
“[^”]*?“ -- miss, that it will find largest srting between two opening quotes (OQ) as possible, in “some text “more text “text it will find “some text “more text “, so you need ? after *.
And as of your pictures, you are using sublime, so replace (“[^”]*?)\s“ with \1”
() capturing a group, which you can access later with \n, where n is group number.
*? lazy expression, stop at first occurence of next character (\s here)
\s any whitespace character (space, tab, new line, etc.)
\1 first captured group, here - opening quote and some text
It is possible to use look behind (?<=text), but it length must be known, in your exampole its length is unknown (because of *).
If you search for s/(“.*?)(“)/, you could replace every second occurrence of “ into ” by r/(“.*?)(“)/$1”/g
.*? as a lazy operator would make it stop right on the second occurrence.
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.