Regex expression that selects only specific word - regex

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

Related

Regexp, I want to extract all the characters including dot(.) that are located between pre selected words

a = regexp(textfile,'apple(\w*)orange','tokens')
I want to extract all the characters between the words apple and orange in text file. One example is;
orangeSEM_DAIM_244.25orange, however when I use may code I got nothing.
I try without the orange so this time I got SEM_DAIM_244 I guess there is a problem with dot(.) configuration. I cant add . to include the dot because when I do it creates problem for the some other words that doesn't includes dot but lies between apple and orange for example SEM_DAIM_A
PS: I am using MATLAB : )
Any idea ?
You can use lookaround assertions to match apple or orange before and after your word, because they are lookarounds and not part of the matching string they will not be included in the matched tokens
((?<=apple)|(?<=orange)).*((?=apple)|(?=orange))
This pattern would match SEM_DAIM_244.25 in all 4 cases
orangeSEM_DAIM_244.25orange
orangeSEM_DAIM_244.25apple
appleSEM_DAIM_244.25orange
appleSEM_DAIM_244.25apple

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

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

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.

How to exclude a certain word in regex?

I'm using this expression and it's perfect for what I need:
.*(cq|conquest).*
It returns any word/phrase/sentence/etc. with the letters 'cq' or the word 'conquest' in it. However, from those matches I want to exclude all that contain the term 'conquest power'.
Examples:
some conquest here (should match)
another cq with some conquest here (should match)
too much cq or conquest power is bad (should not match)
How can I do that to the regex above? It has to be only one regex otherwise the program that I'm using (Advanced Combat Tracker) will create two different tabs.
If you want to match any string which contains "conquest" or "cq", but not if the string contains "conquest power", then the regex is
^(?!.*conquest power).*?(?:cq|conquest).*
The above will attempt to match from the start of the string to the end of the line, if you want to match from the start of each line, switch on multiline mode if available - adding (?m) to the start of the regex may do that.
If you want to match across newlines change . to [\s\S], or switch on singleline mode if available.
You have confused people by stating "I want to match 'cq' or 'conquest'" but also "I want the regex to extract that line".
I assume you don't really want to match just "cq" or "conquest", you want to match strings/lines (?) containing "cq" or "conquest".
From your original question I got that you want to match all strings which contain "cq" or "conquest" but do not contain "power". For this case the following regexp works:
^([^p]|p(?!ower))*(cq|conquest)([^p]|p(?!ower))*$
(regexpal)

regex match string replace in new string (notepad )

In Notepad++ I need to match "dog" (search) in
<tag>old-string/dog.swf">more-old-string</tag>
then use a back-reference (\1) to include it in another string (replace):
new-string_\1>more-new-string
to give the result
new-string_dog.swf">more-new-string
I'm new to regex, so please show me how to do this first by matching "dog", then by excluding all old-string in the result.
Edit: I realize this might be confusing, so I posted the actual problem here: regex find word in string, replace word in new string (using Notepad++). I hope it makes more sense.
I am totally going off a limb here trying to understand what you want, but if I have understood you correctly you want to:
match a certain word in one string
create a new string with data from the old one, under the old string which should be untouched
If so, this regular expression: /^(.*?/([^"]+)\.swf)(".*$)/i with this replacement: \1\3\r\n<test>\2</test> should do the trick. I have used <test></test> to show you where you put your "new-string" stuff.
I hope this helps!