Search regex for Notepad++ - regex

I am looking to create a regex for searching Notepad++
I have a notepad page with thousands of random codes such as:
415615610230
151156125611
161651651516
511111115165
I need to search the entire notepad for multiple codes with once search
I know the regex would look like (415615610230|151156125611|161651651516)
but what I need to do is build a regex like above by pasting in all my search criteria.
If I have say 100,000 numbers I might need to search the 100,000 numbers for 20 codes/numbers.
lets just say I want to search for
5155584865
5155584866
5155584867
5155584868
5155584869
5155584870
5155584871
5155584872
5155584873
5155584874
5155584875
5155584876
5155584877
5155584878
5155584879
5155584880
5155584881
5155584882
5155584883
5155584884
The regex should look like:
(5155584865|5155584866|5155584867|5155584868|5155584869|5155584870|5155584871|5155584872|5155584873|5155584874|5155584875|5155584876|5155584877|5155584878|5155584879|5155584880|5155584881|5155584882|5155584883|5155584884)
Is there a way to build the regex above by just pasting in
5155584865
5155584866
5155584867
5155584868
5155584869
5155584870
5155584871
5155584872
5155584873
5155584874
5155584875
5155584876
5155584877
5155584878
5155584879
5155584880
5155584881
5155584882
5155584883
5155584884
Or can anyone recommend an easier way to search the entire notepad document?

If you just want to search for the template above (e.g. starting with 51555848) the you can do
/51555848.([^\s]+)/g
This will match everything starting with 51555848 and ending with a whitespace.

copy your space separated numbers in a new document in your notepad++ and then replace all spaces or whitespaces (\s) with the pipe symbol (| or \| if your search mode is regex).
And you do not need the round brackets for your search string
EDIT:
Instructions for converting a list of numbers (line separated) into a regex
mark everything (ctrl + a)
join rows (ctrl + j)
replace (ctrl + h) with
search pattern: \s+
replace pattern: \|
search mode: Regex

Related

gVim reg exp: How to search with saved pattern

I have some patterns like
a,10
a,12
a,13
b,20
b,22
c,30
d,33
I want to convert to
a,10,12,13
b,20,22,0
c,30,0,0
d,33,0,0
using gVim regexp.
Is it possible to search with saved patterns in gVim regular expression? Like
%s/\\(.\*\\),\\(.\*\\)\n\1..../\1,\2/gc
Or is there any other method to achieve this?
Convoluted but following would work
:%s/\v\d+$\zs\n\w+
:%s/\d\zs$/,0,0,0
:%s/\v^\w+(,\d+){3}\zs.*$
:%s/\v\d+$\zs\n\w+
search all lines ending with a digit
followed by a newline
starting with a word
and removes the newline and word
:%s/\d\zs$/,0,0,0
Add three 0's to each line ending with a digit
:%s/\v^\w+(,\d+){3}\zs.*$
Removes until the end line after the 3th matching comma/digit pair

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]+)

Numbers & escape characters for regex replacement in Notepad++

I'm trying to match a pattern using RegEx in notepad++, but not having much luck. I'm able to match part but not all of it.
I need to search for this line:
<size value="Large" pax="13074"/>
And replace it with this:
<size value="Very_large" pax="41450" cargo="Largest" cargovolume="3227"/>
Essentially I need to find all patterns matching pax="n"/> and replace them with pax="n" cargo="Largest" cargovolume="0"/> while retaining the initial value of n.
So, ideas anyone?
Press Ctrl + F, move to tab Replace, in Find what do: pax="(\d+)" and in Replace with put this: pax="\1" cargo="Largest" cargovolume="0"
Remember to mark regex. That should retain the number and replace the content.
UPDATE: Hint about saving text for replacement.
Whenever you use regex to do text replacement, wrap the content you want to save in parenthesis and then you can access them using \i where i is the order of appearance of the parenthesis starting at 1.
Hope it helps!

Regex to search a multi-line field in a text file

I have a text file of logs. In it I am interested in searching a field using some regular expression (I use notepad++ on Win, but even use vim on Ubuntu to parse/read this log text file so either one is ok)
The text file has entries as below.
src.type= DEVICE_1 <-- there is a space and then a newline char after the last letter which is 1
dst.type= ZONE_1
someparam1
src.type= DEVICE_1
dst.type= ZONE_2
someparam2
Such entries keep repeating in the log text file.
I am interested in finding those lines which have DEVICE_1 in it but only for those occurrences which have a dst.type= ZONE_2 after it i.e.
I intend to find
src.type= DEVICE_1
dst.type= ZONE_2
but not
src.type= DEVICE_1
dst.type= ZONE_1
Notepad++ allows searching using keywords as regexes. I could get a working regex or any other way (not necessarily involving regexes) to find such occurrences I am looking for in the text file.
I tried below in notepad++ search using regex without success:
src.type= DEVICE_1 \ndst.type= ZONE_2
Also tried [ ] character class.
How can I search for what I am looking to find?
In Vim, the following pattern seems to match what you want:
DEVICE_1\s*\n.*ZONE_2
Use /DEVICE_1\s*\n.*ZONE_2 to jump to the next match.
Use :g/DEVICE_1\s*\n.*ZONE_2/command to execute command on each match.
Use :vim DEVICE_1\s*\n.*ZONE_2 % | cw to list all the matches in the quicfix window.
Note that you can easily reuse the latest search pattern with //. It is a common strategy to work on your search pattern with /foo and, once you are satisfied, perform a substitution like this:
:%s//bar
In Notepad++, use the following regex, with the ". matches newlines" checkbox enabled:
src.type= DEVICE_1\s+dst.type= ZONE_2
There you go for Vim:
/^\zssrc.type= DEVICE_1\ze\_.\{2,2}\_^dst.type= ZONE_2$/
Breakdown of important expressions:
\zs - Start match here (will be highlighted from here);
\ze - End match here (will be highlighted to here);
\_. - same as ., but new line is also included;
\_^ - like ^, but \_ is required because we are in the middle
of regular expression.
For others, I'd refer you to Vim's documentation.

Search and replace regular expression in Open Office calc

I've got something like this (in Open Office Calc):
Streetname. Number
Streetname. Number a
etc.
Now I want to delete everything in front of the number.
So I need to do a search and replace I guess.
^.*?([0-9])
this one matches Streetname. Number .. but what should I put in the replace field?
If I do the search and replace, it deletes everything within the datafield :(
In Search for field, write the following regex: (.*?[:space:])([0-9]+)
And in Replace with, write: $2
That means that you search for:
any characters followed by a space
one or more digits.
Replace all that with $2 - the reference to the digits.
It will replace Streetname. Number 24 with 24. Why did you put a in your example?