Regular Expression to find specific section of a text file - regex

I want to find a section from text file using regular expression. I have file as below:
This is general text section that I don't want.
HEADER:ABC1
This is section text under header
More text to follow
Additional text to follow
HEADER:XYZ2
This is section text under header
More text to follow
Additional text to follow
HEADER:KHJ3
This is section text under header
A match text will look like this A:86::ABC
Now, I want to retrieve all section text up to HEADER if the section text contains the match A:86::ABC. The result text will be
(HEADER:KHJ3
This is section text under header
A match text will look like this A:86::ABC).
I appreciate any help. I am using python and the match section can be more than one in a file. Also this is a multi line file.

regex = re.compile(".*(HEADER.*$.*A:86::ABC)", re.MULTILINE|re.DOTALL)
>>> regex.findall(string)
[u'HEADER:KHJ3\nThis is section text under header \nA match text will look like this A:86::ABC']
Hopefully this helps.
For 2 captures use ".*(HEADER.*$)(.*A:86::ABC)"

Related

VSCode - Regex to find string excluding others substrings

I have a base classname form my icons, that is i-svg (this is the default icon size).
I have some modifiers to change the size, like i-svg--xs or i-svg--sm.
I need to find only the lines that don't use the modifiers classnames, that is just i-svg.
Excluded searches
class="someclass1 i-svg i-svg--xs someclass2"
class="someclass1 i-svg someclass2 i-svg--xs"
class="i-svg i-svg--xs"
class="i-svg i-svg--xs someclass1"
class="someclass1 i-svg i-svg--xs someclass2"
Included searches
class="someclass1 i-svg someclass2"
class="someclass1 i-svg"
class="i-svg someclass1"
class="i-svg"
What regular expression can I use for this search?
the problem is that i-svg is a prefix of the string not allowed
You can do it in a 2 step find
Prepare a file with the following 2 regex strings
class="[^"]*i-svg[^"]*"
^((?!i-svg--(xs|sm)).)*$
Second regex based on SO answer
select the first regex: class="[^"]*i-svg[^"]*"
go to the file where you want to find
search for class="[^"]*i-svg[^"]*"
press Alt+Enter to select all matches and put focus in editor
go to other file and pick up: ^((?!i-svg--(xs|sm)).)*$ with Ctrl+C
go back to the file where you want to find
in find dialog press button: Find in Selection
replace regex with: ^((?!i-svg--(xs|sm)).)*$ with a Ctrl+V
Now only the class attributes with only i-svg are found.

VBscript regular expression

There is a txt file containing multiple lines with - Browser("something").page("something_else").webEdit("some").
I need to retrieve the names of the browser, page and fields (names surrounded by double quotes ) and replace the line with "something_somethingelse_some" (concatinating the names of the browser, page n filed respectively), please help.
the names can be anything so we should go with regex. Note we have to convert everything comes in the above format within the text file till the EOF..
You may try this:
^Browser\("(.*?)"\).page\("(.*?)"\).webEdit\("(.*?)"\).*$
and replace by:
$1_$2_$3
Regex Demo

Finding the ISBN pattern by regexp in Notepad++

I have numerous text files that have book data in which I am trying to extract the International Standard Book Number (ISBN) from. Example snippets:
{" , "classifications": {}, "title": "La casa", "identifiers": {}, "isbn_13": ["978-84-940533-7-5"], "covers": [7281722], "created": {"type": "/type/datetime",
and
"2014-07-28T06:07:52.898549"}, "number_of_pages": 408, "isbn_13": ["9789602354292"],
but how would I go about finding and extracting that ISBN information? Some of the ISBN numbers have dashes, and some do not. Is there a way to replace everything in the text file with a blank except for the snippets that match? I've done research on several similar questions, but having a hard time comprehending it all as I am very new to Notepad++.
Lets say you have your ISBN and some more text in a text file line by line you'd go through following steps:
Make a copy of your text file first!
Open your text file in Notepad++.
Ctrl+H
Search mode: Regular expression
Find what: ^.*?(((1[03])*[ ]*(: ){0,1})*(([0-9Xx][- ]*){13}|([0-9Xx][- ]*){10})).*
Replace with: \1
Click on Replace All
For RegEx please search Google or StackOverflow first. For further information have a look at RegExLib.com, the Internet's first Regular Expression Library.

Need to highlight particular pattern of text

I have a file with some paragraphs, what i want to do is to highlight certain pattern of text/words occurring in the text file with background yellow and text color black.
pattern = ["enough", "too much"];
Text file = "text.txt";
and show it on a webpage with highlighted text for enough and too much words in the text file.
I want to use perl to do this task.
Please tell me how i can do this in optimized way.
Make array of all the words you want to highlight.
Save input file in $file variable.
run foreach on that array and use regular expression to replace the word with word+HTML tag.
ie...
foreach(#words)
{
$file=~r/$/< font color=black, bgcolor=yellow>$< /font>/g;
}
save the $file again as a file with .html or .htm extension.
This was more like logic question than technical i guess.

Notepad++ : Search for static value and replace it with value from list

1 File:
<DisplayName>**just_an_example**</DisplayName>
<DisplayName>**just_an_example**</DisplayName>
<DisplayName>**just_an_example**</DisplayName>
<DisplayName>**just_an_example**</DisplayName>
2 File
**example1**
**example2**
**example3**
**example4**
What do I need:
In notepad++ native search/replace dialog, search for "just_an_example" and replace it with the values from 2nd file, in sequence.
Output:
<DisplayName>**example1**</DisplayName>
<DisplayName>**example2**</DisplayName>
<DisplayName>**example3**</DisplayName>
<DisplayName>**example4**</DisplayName>
Is this possible, to search in all active files under Notepad++. Possible not using Python Script?
Your question implies that the content of file 1 is not relevant (except for the DisplayName tags).
So replace the content of file 1 by pasting all of file 2 and replace new lines by the tags. You can do that in Notepad++ by searching for \n and replacing it by </DisplayName>\n<DisplayName>. All that is left then is manually fixing the first and last line.