Regex remove everything before the first space occurrence in a line? - regex

I would like to remove all the characters before the first space occurrence for each line.
Sample of initial text:
2:2 My dog is good.
1:234 My cat is bad
14:2 My frog is bad but it loves my garden.
Result must be:
My dog is good.
My cat is bad
My frog is bad but it loves my garden
What regular expression would you use to achieve this result using OpenOffice Calc or Notepad++?

Ctrl+H
Find what: ^\S+\s+(.+)$
Replace with: $1
check Wrap around
check Regular expression
DO NOT CHECK . matches newline
Replace all
Explanation:
^ : beginning of line
\S+ : 1 or more non space character
\s+ : 1 or more space character
(.+) : group 1, 1 or more any character (ie. rest of the line)
$ : end of line
Replacement:
$1 : content of group 1
Result for given example:
My dog is good.
My cat is bad
My frog is bad but it loves my garden.

Press Ctrl + h to open Find and replace dialog
Write ^.*?\s+(.*)$ in 'find what' textbox
Write $1 in 'Replace With' textbox
Check Regular Expression radio button OR press ALT+g
You can click on Find Next button to verify whether its working correctly (to find matches)
Click on Replace All button if it looks good OR press ALT+a
Explanation:
^ : Match from beginning of line
.*?\s+: Match anything, any number of times until a space (or more spaces) is encountered
(.*): Capture everything after those spaces
$: Match till end of the line
$1: Access the above captured strings from line

Related

How to replace specific character one time

I want to replace character - using regular expression in my text so it would work like this:
Original text: abcd-efg-hijk-lmno
Text after replacing: abcd-efg-hijk/lmno
As you can see I want to replace character - starting from the end just one time with character /.
Thanks in advance for any tips
Find what: -([^-]*)$
Replace with: /$1
Search Mode: Regular Expression
Explanation:
- : a dash
([^-]*$) : text with no dash,
zero or more times,
to the end of the line,
put in the $1 variable
/$1 : literal "/", contents of $1
Good resource: http://www.grymoire.com/Unix/Regular.html
To replace characters in Notepad++, you can open the Replace window using Ctrl+H, or under the "Search" menu. Once open, enter the following regular expression:
(.{4}-.{3}-.{4})(-)(.{4})
This will find:
a group of four characters (the "." being any character, the "{4}" being the quantity),
a dash,
a group of three characters,
another dash,
a group of four characters,
again another dash,
then a group of four characters.
The parentheses group this search into captured groups, which we will use for the replacement part. See https://www.regular-expressions.info/brackets.html for more info.
If you want to restrict the search to lowercase letters as in your example, you would replace the "." with "[a-z]", or for upper and lower "[a-z,A-Z]".
Now for the replacement. The groups from earlier are referenced by the dollar sign then the number, e.g. $1 would be the first. So we will replace the characters found with the first group ($1), disregard the second group containing the dash and insert the "/" instead, then include the third group ($3):
$1/$3
The settings in the replace window need to have "Regular expression" and "Wrap around" checked, and ". matches newline" unchecked.
You can then click Replace all to replace all occurrences, or go through using Replace individually.
Since the beginning and end of line characters are not included, you can find multiple occurrences of this pattern on a single line.
Note: This answer follows the same procedure as Toto's, however uses a different regular expression.
Ctrl+H
Find what: ^(.+)-([^-]+)$
Replace with: $1/$2
check Wrap around
check Regular expression
DO NOT CHECK . matches newline
Replace all
Explanation:
^ : begining of line
(.+) : 1 or more any character, catch in group 1
- : a dash
([^-]+) : 1 or more any character but dash, catch in group 2
$ : end of line

Notepad++ Regex Mark

I have a list like this
randomtext.html
file1
file1
file2
I want to mark the first line after the line ended with html. So, it should marked the first "file1" only
Have a try with:
Within Mark tab
Find what: html\R\K.+
Be sure you haven't chck . matches newline and check Mark lines then click on Find All
Explanation:
html : Literally html
\R : any kind of line break (ie. \n or \r or \r\n)
\K : forget everything found before
.+ : Everything until the next line break. (ie. the second line).
First of all i would like to ask what do you mean by mark?
Press ctrl+h. Select regular expression and you can use following regular expression:-
Find: (.* )html\r\n(.)\r\n(.)
Replace: $1html\r\n-------$2\r\n$3
Here i have use '-------' as the mark, you can put the mark according to your need.

Add to end of line that contains a specific word and starts with x

I would like to add some custom text to the end of all lines in my document opened in Notepad++ that start with 10 and contain a specific word (for example "frog").
So far, I managed to solve the first part.
Search: ^(10)$
Replace: \1;Batteries (to add ;Batteries to the end of the line)
What I need now is to edit this regex pattern to recognize only those lines that also contain a specific word.
For example:
Before: 1050;There is this frog in the lake
After: 1050;There is this frog in the lake;Batteries
You can use the regex to match your wanted lines:
(^(10).*?(frog).*)
the .*? is a lazy quantifier to get the minimum until frog
and replace by :
$1;Battery
Hope it helps,
You should allow any characters between the number and the end of line:
^10.*frog.*
And replacement will be $0;Batteries. You do not even need a $ anchor as .* matches till the end of a line since . matches any character but a line break char.
NOTE: There is no need to wrap the whole pattern with capturing parentheses, the $0 placeholder refers to the whole match value.
More details:
^ - start of a line
10 - a literal 10 text
.* - zero or more chars other than line break chars as many as possible
frog - a literal string
.* - zero or more chars other than line break chars as many as possible
try this
find with: (^(10).*(frog).*)
replace with: $1;Battery
Use ^(10.*frog.*)$ as regex. Replace it with something like $1;Batteries

search and replace \n or \t with and empty space

Can anyone teach me how to search and replace \n or \t and replace it with an "empty" space? I tried using the search and replace in GEDIT, but it just doesn't change. I'm using GEDIT by the way.
sample:
Hi I am \n\t\t\t\t\t\t\t\t\ John Doe`n\t\t\t\t\t` I live in BLAH BLAH.
Output:
Hi I am John Doe I live in BLAH BLAH.
How to do this?
Although this thread is now 5 years old the answers do not really suffice so here is a working answer;
The most simple way to replace newline and tab characters (\n & \t) use the backslash to tell gEdit search you don't want to search for an actual newline but for the chars that represent a new line;
\\n or \\t
Replace \s+ with a single space.
\s means 'any white space character'
+ means 'one or more'
So \s+ means 'one or more white space characters'. You'd want to replace those with a singe space.
Replace text in gedit
Open the Replace tool by clicking Search ▸ Replace or press Ctrl+H.
Enter the text that you wish to replace into the 'Search for:' field.
Enter the new, replacement text into the 'Replace with:' field.
Once you have entered the original and replacement text, select your
desired replacement options:
To replace only the next matching portion of text, click Replace.
To replace all occurrences of the searched-for text, click Replace
All.
Source

Regex to replace a character between two l

My text file has more than ten thousand lines. Each line starts with a word or a phrase followed by a tab and the content, such as:
[line 1] This is the first line. [tab] Here is the content.[end of line]
I want to find character s in all the words between the beginning of each line and a tab (\t), and replace it by a pipe (|) so that the text will look like:
[line 1] Thi| i| the fir|t line. [tab] Here is the content.[end of line]
Here is what I have done:
Search: ^(.*)s+(.*)?\t
Replace: \1|\2\t
It works but the problem is it does not replace s in one replace. I have to click on Replace All for several times before s in all the words is replaced.
So it comes to my question: how can I replace all the occurrences of character s in just one search and replace?
Note that I'm working on TextWrangler but I'm OK with other editors.
Thanks a lot.
You are searching for lines containing an s and do the match. Instead you should be searching for the s directly, and use lookahead to ensure that it is followed by a tab.
Search: s(?=.*\t)
Replace: |
Note that this catches all s's up to the last tab. - This will be a problem if your main content can contain tabs.
To stop catching s's after the first tab you have to cheat. Since variable length negative lookbehind doesn't work in AFAIK any regexp dialect.
However if we can ensure that the last s catches the whole line...
Search: (?:(^[^s\t]*\t.*$)|s([^s\t]*(?:(?=s.*\t)|\t.*$)))
Replace: |\1\2
This will catch the whole line in the case where no s occurs before the first tab. And put a | in front of that line. I see no way around this.