Regex replacing word within string beginning with specific word (Notepad++) - regex

I want to replace each occurrence of a specific word, but it has to be in a line which begins with another certain word.
Example text:
This is some random text here
That is also some random text here
I want only to select lines beginning with "This" and change the "text" to e.g. "word".
Result of fin&replace in Notepad++ would be:
This is some random word here
That is also some random text here
So far, I was able to select the line, no problem there: (This.+)
The problem is how to search for and replace the word "text", since I can't get the group/sub-pattern to work within itself, using \1.
I was able to select a string from and to a certain word, but can't figure out how to search within a line that is found.
I'm a regex rookie, so have patience. :)
Many thanks for sharing your brilliant thoughts!

^This\b.*?\K\btext\b
Try this.Replace by word.See demo.
https://regex101.com/r/jV9oV2/9
or
^(This\b.*?)\btext\b
Replace by \1.

Related

How can I delete this part of the text with regex?

I have a problem that I really hope that somebody could help me. So, I want to delete some parts of text from a notepad++ document using Regex. If there's another software that I can use to delete this part of text, let me know please, I am really really noob with regex
So, my document its like this:
1
00:00:00,859 --> 00:00:03,070
text over here
2
00:00:03,070 --> 00:00:09,589
text over here
3
00:00:09,589 --> 00:00:10,589
some numbers here
4
00:00:10,589 --> 00:00:12,709
Text over here
5
00:00:12,709 --> 00:00:18,610
More text with numbers here
What I want to learn is how can I delete the first 2 lines of numbers in all the document? So I could get only the text parts (the "text over here" parts)
I would really appreciate any kind of help!
My solution:
^[\s\S]{1,5}\d{1,3}:\d{1,3}:\d{1,3},\d{1,5}\s-->\s*?\d{1,3}:\d{1,3}:\d{1,3},\d{1,5}\s
This solution match both types: either all data in one line, or numbers in one line and data in the second.
Demo: https://regex101.com/r/nKD0DQ/1/
Simplest solution;
\d+(\r\n|\r|\n)\d{2}:\d{2}.*(\r\n|\r|\n)
Get line with some number \d+ with its line break (\r\n|\r|\n)
Also the next line that starts with two 2-digit numbers and a colon \d{2}:\d{2} with the rest .* and its line break. No need to match all since we already are in the correct line, since subtitle file is defined well with its predictable structure.
Put this as Find what: value in Search -> Replace.. in Notepad++, with Seach Mode: Regular Expression and with replace value (Replace with:) of empty space. Will get you the correct result, lines of expected text with empty line in between each.
to see it on action on regex101
Subtitles, for accuracy you can use this:
\d+(\r\n|\n|\r)(\d\d:){2}\d\d,\d{3}\s*-->\s*(\d\d:){2}\d\d,\d{3}(\r\n|\n|\r)
Check Regular Expression, Find what with this and Replace with empty would do.
Regxe Demo
srt subtitles are basically ordered. And it's better accurate than lose texts.
\d : a single digit.
+ : one or more of occurances of the afore character or group.
\r\n: carriage and return. (newline)
* : zero or more of occurances of the afore character or group.
| : Or, match either one.
{3}: Match afore character or group three times.
I'm going for a less specific regex:
^[0-9]*\n[0-9:,]*\s-->\s[0-9:,]*
Demo # regex101

Remove columns from CSV

I don't know anything about Notepad++ Regex.
This is the data I have in my CSV:
6454345|User1-2ds3|62562012032|324|148|9c1fe63ccd3ab234892beaf71f022be2e06b6cd1
3305611|User2-42g563dgsdbf|22023001345|0|0|c36dedfa12634e33ca8bc0ef4703c92b73d9c433
8749412|User3-9|xgs|f|98906504456|1534|51564|411b0fdf54fe29745897288c6ad699f7be30f389
How can I use a Regex to remove the 5th and 6th column? The numbers in the 5th and 6th column are variable in length.
Another problem is the User row can also contain a |, to make it even worse.
I can use a macro to fix this, but the file is a few millions lines long.
This is the final result I want to achieve:
6454345|User1-2ds3|62562012032|9c1fe63ccd3ab234892beaf71f022be2e06b6cd1
3305611|User2-42g563dgsdbf|22023001345|c36dedfa12634e33ca8bc0ef4703c92b73d9c433
8749412|User3-9|xgs|f|98906504456|411b0fdf54fe29745897288c6ad699f7be30f389
I am open for suggestions on how to do this with another program, command line utility, either Linux or Windows.
Match \|[^|]+\|[^|]+(\|[^|]+$)
Repalce $1
Basically, Anchor to the end of the line, and remove columns [-1] and [-2] (I assume columns can't be empty. Replace + with * if they can)
If you need finer detail then that, I'd recommend writing a Java or Python script to manual parse and rewrite the file for you.
I've captured three groups and given them names. If you use a replace utility like sed or vimregex, you can replace remove with nothing. Or you can use a programming language to concatenate keep_before and keep_after for the desired result.
^(?<keep_before>(?:[^|]+\|){3})(?<remove>(?:[^|]+\|){2})(?<keep_after>.*)$
You may have to remove the group namings and use \1 etc. instead, depending on what environment you use.
Demo
From Notepad++ hit ctrl + h then enter the following in the dialog:
Find what: \|\d+\|\d+(\|[0-9a-z]+)$
Replace with: $1
Search mode: Regular Expression
Click replace and done.
Regex Explain:
\|\d+ : match 1st string that starts with | followed by number
\|\d+ : match 2nd string that starts with | followed by number
(\|[0-9a-z]+): match and capture the string after the 2nd number.
$ : This is will force regex search to match the end of the string.
Replacement:
$1 : replace the found string with whatever we have between the captured group which is whatever we have between the parentheses (\|[0-9a-z]+)

Regex expression that selects only specific word

Welcome guys, I am just new to this community!
Here is the case, I am having some strings like these
thatisanappleaaa
thatisanappleaaa bad
thatisanappleaaa.bad
thatisanapplebadaaa
thatisanbadappleaaa
thatisanbadbadappleaaa
badthatisanappleaaa
and trying to use Sublime Text 3 Find and replace function to achieve the following (note that only the first line is being replaced)
thatisanorangeaaa
thatisanappleaaa bad
thatisanappleaaa.bad
thatisanapplebadaaa
thatisanbadappleaaa
thatisanbadbadappleaaa
badthatisanappleaaaa
Is there a regex that filters "apple" in "thatisanappleaaa"(which is line one) only without the presence of "bad" in any position (except between "apple") in the string, given that the string "bad" does not change every time it appears?
Try
(\w+)apple(\w+).*
will select all text wrapped around apple
if you want to select text trailing after apple use
apple(\w+).*
After reading your description I'm assuming you want to replace the word apple only in sentences which do not have any occurrences of the word bad.
I've used a regex which uses a negative lookahead and used parentheses to capture apple which can then be replaced with any word, in your case orange.
Regex: ^(?!.*bad).*(apple)
DEMO

Issue with selecting items using regex

I have text separated by white spaces and a search range of more than 1000 words.
Approximately 70% of the words are following this pattern foo-bar-...-N, where N is unknown value for counter for words written between this sign: "-". After each word(between each word) there is a blank space.
What I need is for the script to select everything after the foo-bar up until the blank space.
I know how to select whole thing, but not how to get solution for my issue.
Here is some example for my idea:
foo-bar foo-bar-thing foo-bar-stuff-my-gosh ... foo-bar-for-educational-purposes
And regex should select them like so:
[foo-bar] [foo-bar]-thing [foo-bar]-stuff-my-gosh ... [foo-bar]-for-educational-purposes
You want a the regex to fetch a phrase and extract a substring from it.
To do that you need a group.
So here is the code you want :
foo-bar([\w-]*)
There is a space at the end don't forget it. You need to set the global flag as you can see in the demo. And your string has to end with a space if you want to match the last one. If it's multiline don't forget the multiline flag too.

Regex: for each /xxx.png go to next /yyy.eps and change it to /xxx.eps

I'd like to get a regex like so:
"for each /xxx.png go to the inmediately next /yyy.eps, and change it to /xxx.eps"
If possible, how could I do it with regex?
I'm working in a CSV file and using Notepad++.
Many thanks!
EDIT
Hoping this helps to clarify, a better example would be:
line 1: "landscape123.png","IwantToBeNamedlandscape123.eps"
line 2: "picture123.png","IdLikeToBeNamedpicture123.eps"
How can I take the pngs filenames and replace the next .eps filenames with them? Each time, both file types are on the same line.
Find:
^"(.*)\.png".*$
Replace with:
"\1.png","\1.eps"
This says: "Find lines that contain exactly: ", a filename (and capture the filename), .png", and then whatever; and then replace them with "\1.png","\1.eps"", where \1 is a backreference that contains the filename.
Make sure you have ". matches newline" unchecked.