I have text like this:
something text
(some text here image and more text)
some more text
(test)
text
I want to do a regex search for everything in between the 2 parenthesis and search for the word image, if that word exists between 2 parenthesis then I want to add a new line AFTER that line. So my regex should produce this output:
something text
(some text here image and more text)
some more text
(test)
text
How can I best achieve this? I've tried (?<=\()(?=image)(?=\)) but that didn't work.
Using a word boundary:
\(.*\bimage\b.*\)
To capture that pattern when matching, place it within parentheses:
(\(.*\bimage\b.*\))
Then try referencing the group using $1 (or \1 depending on the language in which the regex is being used).
you didn't mention a tool, but with sed
sed 's/(.*image.*)/&\n/' file
if you want to restrict to standalone word "image" use \b word boundary (I think GNU sed only though)
sed 's/(.*\bimage\b.*)/&\n/' file
You can use the following regex to search for the word image in between parentheses and by replacing it with the captured group and a new line you can get the expected result :
(\(.*?image.*?\))
input >> something text
(some text here image and more text)
some more text
(test)
text
regex search >> (\(.*?image.*?\))
replace with >> `$1\n`
output >> something text
(some text here image and more text)
some more text
(test)
text
see demo / explanation
Related
I want to learn more about the structure and use of regex paths. My question is, using regex commands, can you enter non present text in another?
I explain, let's say, I have some text and I want to "add text" using a regex commands:
Some text : these house is red
Final text: these house is red and blue
I know the replace function is the solution
search: (.*)
replace: $1 and blue
but is there any way to enter that text using the regex command instead of using the replace function?
search: (.*)($1 and blue)
replace: $1$2
Not sure which language you are using, but this answer applies to Python. Since you are only replacing text at the end of line, this should suffice (all you should need to do is modify the regex formula):
import re
s = 'these house is red'
s_new = re.sub(r'$', ' and blue', s)
print(s_new)
OUTPUT:
these house is red and blue
Note that the regex used here simply matches the end of the line (or string in this case):
r'$'
I ran a dir /s command to list files in folders & sub-folders, and to show the file size. My issue is that I use a "," as a separator between the file name and size.
Is their any way to find the last comma in the and replace it with";"?
My text file looks like this:
H:\IP Phones, Mobile Information.xls,152064
H:\Master Sheet Updated.xlsx,46446
The following regex will match a comma that is not followed by .* (any number of any character) and a comma.
,(?!.*?,)
Then just use the Replace... functionality in Sublime (Ctrl+H) and it should work.
I'm looking for a way to lowercase selected BACKTICKED text using a regex in Notepad++
Here's an example string:
Select * FROM `Weird_TEXT` WHERE
And the desired output:
Select * FROM `weird_text` WHERE
I can find these strings (including the backticks) using the regex:
(?<=FROM )(.*)(?= WHERE)
`Weird_TEXT`
But I can't figure out how to replace the text as lowercase, I've tried using the following:
\1\L\2\L
\1\L\2\L\3
EDIT:
When I click Find Next in Notepad++, the backticks around the word Weird_TEXT are included in the selection. Could this be why the RegEx isn't working?
Just put this in the replace box:
\L$1
I have a large text file with numerous lines containing data like below.
205=1<SOH>55=ES<SOH>48=17875701615154475972<SOH>207=CME<SOH>100=XNYM<SOH>16552=1
205=155=6A48=17875736456456445774207=CME100=XNYM16552=1
I would like to extract all of the values that are after the "48=" and before the ASCII code 01 delimiter AND the same for the value after "55=" and paste them into a new file:
ES|17875701615154475972
6A|17875736456456445774
They aren't all 20 characters in length, so I would need to do a regex search to mark them all - can you help me with the right regex expression to use and how to copy the identified values out of notepad++?
Do a replacement on the whole file to leave only the targets:
Find: ^.*\b48=(\d+).*
Repl: $1
Then ctrl+a, ctrl+c and paste into a new file.
To answer the question in the comment about capturing "CME" and allowing both "55" and "48" as markers:
Find: ^.*?(?:48|55)=([\w;]+).*?=([A-Z]+).*
Repl: $2|$1
The following will match and create a group for the digits. <SOH>48=(\d*)<SOH>
However, what you probably want is a global search/replace that finds the numbers and rewrites the file. Try
Find: .*<SOH>48=(\d*)<SOH>.*
Replace: \1
Of course remember to check the Regular Expression box or it won't work at all.
I have many text files and need to locate certain words that may exist in the context of the file but need only those that are in quotation marks.
Example: Find in the text below the word "search" only if in quotes (the word "search" may vary).
1. text text text text text text search text
2. text "search text text text text" text
3. text "SEARCH text text text text" text
For this precise example, I would expect only the words of line 2 and 3.
Thanks to anyone who can help me.
If you can guarantee that there'll be only one set of quotes, then
/".*search.*"/i
should do. But if there can be more than one pair of quotes, then you have to ensure that an even number of quotes have been passed, lest you mistake a closing quote for an opening quote:
/^[^"]*("[^"]*"[^"]*)*"[^"]*search[^"]*"/i
Here's a demo. (Note that the demo contains \ns purely for presentation purposes.) If you see two #s in the demo regex, please replace them with parentheses ( )—it is a limitation of the way RegexPal encodes its data in the URL.
I you want all waords between double quotes, I would simply use grep:
grep -E -o '".*"' inputfile
I f you want only the first word:
sed -E 's/.+"([[:alpha:]]+) .*/\1/' inputfile