Regex remove everything else except one number - regex

I have a string consists of multiple lines (output from powershell) like
....
junk line
junk line
MyVaraible=xxxx
junk line
junk line
....
I need to use one regex to get rid of all the junk lines and extract the variable value.
It is super easy if I can loop thru all the lines where I can just do
"MyVaraible=(\d+)" replace with "$1"
But I'm being super restricted by this ancient system where one regex replacement is all I am allow to do.

You may use this regex based replacement.
Search using this regex:
/^\w+=(.*)$|.*(\n|\z)/
Replace using back-reference:
$1
RegEx Demo
RegEx ^\w+=(.*)$|.*(\n|\z) matches a name-value pair separated by = or it matches a full line followed by line-break or end of string.

Related

finding pattern between two "CERRADO}" strings using negative look-ahead

I have a text file containing lines like these:
CERRADO}165856}TICKET}DESCRIPTION}some random text here\r\n
other random text here}158277747\r\n
CERRADO}165856}TICKET}FR2CODE}more random text also here}1587269339\r\n
My ultimate goal is to concatenate those lines not beginnning with "CERRADO}" string with their preceding line. There might be an arbitrary number of lines not beginning with that string on the file. This is the end result:
CERRADO}165856}TICKET}DESCRIPTION}some random text here other random text here}158277747\r\n
CERRADO}165856}TICKET}FR2CODE}more random text also here}1587269339\r\n
My first attempt was to create a simple regex to match those lines.
CERRADO\}.+\r\n(?!CERRADO\})(.+\r\n)+
After having that regex right, to create a matching group and replace it getting rid of the \r\n patterns, here is what I have so far:
The proposed regex matches all the lines in the file and not just the wanted ones.
Any ideas would be appreciated
You may use
\R(?!CERRADO\})
and replace with a space.
The regex matches:
\R - a line break sequence that is...
(?!CERRADO\}) - not followed with CERRADO}.
Or,
^(CERRADO\}.*)\R(?!CERRADO\})
and replace with \1 . This regex matches:
^ - start of a line
(CERRADO\}.*) - Capturing group 1 (later referred to with \1 backreference from the replacement pattern): CERRADO} substring and then the rest of the line
\R - a line break sequence
(?!CERRADO\}) - not followed with CERRADO}.
To make multiple replacements with this one, you will need to hit Replace All several times.

Notepad++ Regex Issue - Remove Number in Line Replace with HTML

I'm a regex newbie so this has been a lot of trial and error but for some reason I can only get this to work sometimes and I'm not sure why. Let me layout what I'm doing. I have a text file that looks like this:
1.Some Text Here
A paragraph of words here.
2.Some More Text Here
A paragraph of words here.
I use this code to find the lines with a number at the beginning:
^[0-9]+.([^.]*)$
Then I replace it with this:
<h2>$1</h2>\r\r
The problem I'm running into is that it usually grabs the line starting with the number but for some reason it will grab the line with the number and the paragraph below it. So instead of putting the </h2> at the end of the line it puts it at the end of the paragraph below.
I displayed all symbols to see if it had something to do with carriage/line returns but everything looks identical from line to line. The paragraph is on its own line and I see CRLF at the end of each line.
The expression [^.] (ie not a literal dot) matches newlines.
Don't match newlines in your capture:
^[0-9]+\.([^.\r\n]*)
Note that I also escaped the dot following the numbers, making it match a literal dot (a naked dot matches any character).
use \2 instead of $2, check "wrap around"tested on notepad++ 5.9.3 (UNICODE)
Not sure what version of notepad++ you're using but your version of the regex works fine for the example that you have ... i use 6.7.9.2
I can reproduce with the following text. Notice the paragraph for line 1 doesn't end in a period.
1.Some Text Here[CR][LF]
A paragraph of words here[CR][LF]
2.Some Text Here[CR][LF]
A paragraph of words here.[CR][LF]
Your regex matches any number of lines that begins with a set of digits, and doesn't end in a period. It could include more than one line. I would recommend this regex: ^[0-9]+\.([^\r\n]*)\r\n.

Remove multiple commas at the end of lines using Notepad++ regex replace

I have a text below with data in a CSV file
2,3
4,5
6,7
When I save an open this in notepad++, it has extra commas like
2,3,,,,
4,5,,
6,7,,,,,
like you see, there are variable number of leading commas,
I tried a regex match using:
/,{2,}/
I have selected the regular expressions combo-box from the search mode in ctrl + H Replace box.
For some reason this is not working. What do I need to do to match multiple comma and not get rid of single comma?
Is there a better way to get this done in notepad++?
Regex:
,{2,}$
Replacement string:
empty string
This will replace two or more trailing commas with an empty string. To remove all the trailing commas then use ,+$ regex.
\d+(?:,\d+)?\K.*$
You can use this.Replace by empty string.This will work with data like 2,3,
See demo.
https://regex101.com/r/iS6jF6/9

Regular expressions: inserting a word and NOT replacing the found key

I have a list of items, such as:
this_thing.ety
other-stuff.ety
34-pairings.ety
I want to do this:
"At the beginning of every line, insert "images/"
so the result of search/replace with reg exp would yield:
images/this_thing.ety
images/other-stuff.ety
images/34-pairings.ety
I am using:
^.
as my anchor to find the beginning of each line but everything I've tried to add "images/" has resulted in actually replacing that first character. I am using Notepad ++, but can use anything.
I thought using ${foo} was on the right track but I'm missing something here.
In a regex ^.is matching begin of line and a character. If you replace this by 'image', first character, which matched, will be replaced. Empty line wont have 'image' but stay identical (they don't match ^.)
Just use ^ as regexp for begin of line
. is the any character symbol, but can only account for one character. You will want to use ^..*$ or ^.+$ if your version of regex allows so that every line that contains at least one character will be fully replaced. With replace, it would look like this
s/^(.+)$/images\/\1/
where the \1 re-inserts the part in parenthesis in the regex. In older versions of regex, try
s/^^\(..*\)$/\1/

what can be the regex for the following string

I am doing this in groovy.
Input:
hip_abc_batch hip_ndnh_4_abc_copy_from_stgig abc_copy_from_stgig
hiv_daiv_batch hip_a_de_copy_from_staging abc_a_de_copy_from_staging
I want to get the last column. basically anything that starts with abc_.
I tried the following regex (works for second line but not second.
\abc_.*\
but that gives me everything after abc_batch
I am looking for a regex that will fetch me anything that starts with abc_
but I can not use \^abc_.*\ since the whole string does not start with abc_
It sounds like you're looking for "words" (i.e., sequences that don't include spaces) that begin with abc_. You might try:
/\babc_.*\b/
The \b means (in some regular expression flavors) "word boundary."
Try this:
/\s(abc_.*)$/m
Here is a commented version so you can understand how it works:
\s # match one whitepace character
(abc_.*) # capture a string that starts with "abc_" and is followed
# by any character zero or more times
$ # match the end of the string
Since the regular expression has the "m" switch it will be a multi-line expression. This allows the $ to match the end of each line rather than the end of the entire string itself.
You don't need to trim the whitespace as the second capture group contains just the text. After a cursory scan of this tutorial I believe this is the way to grab the value of a capture group using Groovy:
matcher = (yourString =~ /\s(abc_.*)$/m)
// this is how you would extract the value from
// the matcher object
matcher[0][1]
I think you are looking for this: \s(abc_[a-zA-Z_]*)$
If you are using perl and you read all lines into one string, don't forget to set the the m option on your regex (that stands for "Treat string as multiple lines").
Oh, and Regex Coach is your free friend.