(Regular Expressions) 2Liner→1Liner - regex

Thank you in advance and sorry for the bad english!
I want
'Odd rows' 'CRLF' 'Even rows' CRLF' → 'Odd rows' ',' 'Even rows' 'CRLF'
Example Input:
0
SECTION
2
HEADER
Desired Output:
0,SECTION
2,HEADER
What I have tried:
Find: (.*)\n(.*)\n
Replace: $1,$2\n
I want ー Easy to see dxf

. matches a newline the same as it matches any other characer, so the first .* is going to gobble up the whole string and leave nothing left.
Instead, use a character group that excludes \n. Also, it's not clear whether your final line terminates with a \n or not, so the Regex should handle for that:
Find
([^\n]*)\n([^\n]*)(\n|$)
Replace
$1,$2$3
Breakdown:
([^\n]*) - 0 or more characters that are not \n
\n
([^\n]*)
(\n|$) - \n or end of string

For you example data you could capture one or more digits in capturing group 1 followed by matching a newline.
In the replacement use group 1 followed by a comma.
Match
(\d+)(?:r?\n|\r)
Regex demo
Replace
$1,

you should match enter and space also, because there may be multiple spaces and new line available in string
try this regex-
"0\nSECTION\n 2\nHEADER".replace(/([\d]+)([\s\n]+)([^\d\s\n]*)/g,"$1,$3")
var myStr = ` 0
SECTION
2
HEADER`;
var output = myStr.replace(/([\d]+)([\s\n]+)([^\d\s\n]*)/g,"$1,$3");
console.log(output);

DXF file ok
ODD line abc...
(AWK)
NR%2!=0{L1=$0}
NR%2==0{print L1 "," $0;L1=""}

Related

Regex to disregard partial matches across lines / matching too much

I have three lines of tab-separated values:
SELL 2022-06-28 12:42:27 39.42 0.29 11.43180000 0.00003582
BUY 2022-06-28 12:27:22 39.30 0.10 3.93000000 0.00001233
_____2022-06-28 12:27:22 39.30 0.19 7.46700000 0.00002342
The first two have 'SELL' or 'BUY' as first value but the third one has not, hence a Tab mark where I wrote ______:
I would like to capture the following using Regex:
My expression ^(BUY|SELL).+?\r\n\t does not work as it gets me this:
I do know why outputs this - adding an lazy-maker '?' obviously won't help. I don't get lookarounds to work either, if they are the right means at all. I need something like 'Match \r\n\t only or \r\n(?:^\t) at the end of each line'.
The final goal is to make the three lines look at this at the end, so I will need to replace the match with capturing groups:
Can anyone point me to the right direction?
Ctrl+H
Find what: ^(BUY|SELL).+\R\K\t
Replace with: $1\t
CHECK Match case
CHECK Wrap around
CHECK Regular expression
UNCHECK . matches newline
Replace all
Explanation:
^ # beginning of line
(BUY|SELL) # group 1, BUY or SELL
.+ # 1 or more any character but newline
\R # any kind of linebreak
\K # forget all we have seen until this position
\t # a tabulation
Replacement:
$1 # content of group 1
\t # a tabulation
Screenshot (before):
Screenshot (after):
You can use the following regex ((BUY|SELL)[^\n]+\n)\s+ and replace with \1\2.
Regex Match Explanation:
((BUY|SELL)[^\n]+\n): Group 1
(BUY|SELL): Group 2
BUY: sequence of characters "BUY" followed by a space
|: or
SELL: sequence of characters "SELL" followed by a space
[^\n]+: any character other than newline
\n: newline character
\s+: any space characters
Regex Replace Explanation:
\1: Reference to Group 1
\2: Reference to Group 2
Check the demo here. Tested on Notepad++ in a private environment too.
Note: Make sure to check the "Regular expression" checkbox.
Regex

Regex to find every second new line (match only new line characters)

Regex to find every second new line (match only new line characters)
Input:
LINE1
LINE2
LINE3
LINE4
LINE5
LINE6
Output:
LINE1LINE2
LINE3LINE4
LINE5LINE6
I have tried \n[^\n]*\n but it matches text as well for replacement and does not give desired output.
I am having issues in matching every second new line character only.
Thanks in advance!
You could use the regular expression
^(.*)\n(.*\n)
and replace each match with $1+$2.
Demo
Alternatively, you could simply match each pair of lines and remove the first newline character. That requires a bit of code, of course. As you have not indicated which language you are using I will illustrate that with some Ruby code, which readers should find easy to translate to any high-level language. Suppose str is a variable holding your multi-line string. Then:
r = /^(?:.*\n){2}/
s = str.gsub(r) { |s| s.sub(/\n/, '') }
puts s
LINE1LINE2
LINE3LINE4
LINE5LINE6
For an even number of lines, you could make use of a positive lookahead to assert what is on the right side is 0 or more times repetition of 2 lines that end with a newline, followed by matching the last line and the end of the string.
In the replacement use an empty string.
\n(?=(?:.+\n.+\n)*.+$)
Explanation
\n Match a newline
(?= Positive lookahead, assert what is on the right is
(?:.+\n.+\n)* Match 0+ times 2 lines followed by a newline
.+$ Match any char except a newline 1+ times and assert end of string
) Close lookahead
Regex demo
Output
LINE1LINE2
LINE3LINE4
LINE5LINE6

Notepad++ remove all non regex'd text

I have a large list of urls that has a unique numeric string in each, the string falls between a / and a ? I would like to remove all other text from notepad++ that are not these strings. for example
www.website.com/dsw/fv3n24nv1e4121v/123456789012?fwe=32432fdwe23f3 would end up as only 123456789012
I have figured out that the following regex \b\d{12}\b will get me the 12 digits, now I just need to remove all of the information that falls each side. I have had a look and found some posts that suggest replace with \t$1 , $1\n
, $1 , and /1 however all of these do the exact oposite of what I want and just remove the 12 digit string.
You can use this regex and replace it with empty string,
^[^ ]*\/|\?[^ ]*$
Demo
Explanation:
^[^ ]*\/ --> Matches anything expect space from start of string till it finds a /
\?[^ ]*$ --> Similarly, this matches anything except space starting from ? till end of input.
Ctrl+H
Find what: ^.*/([^?]+).*$
Replace with: $1
check Wrap around
check Regular expression
UNCHECK . matches newline
Replace all
Explanation:
^ # beginning of line
.* # 0 or more any character but newline
/ # a slash
([^?\r\n]+) # group 1, 1 or more any character that is not ? or line break
.* # 0 or more any character but newline
$ # end of line
Result for given example:
123456789012

Notepad++ How to remove a lines containing 3 same characters in order

lemme show an example. My file looks like this:
AaaAab
AacAaa
AacAap
AaaBbb
I would like to delete all the lines which contains 3 same characters in first or second 3 chars. Which means I will receive only AacAap from above example.
You can use something like:
^(?:(.)\1\1.*|.{3}(.)\2\2.*)$
Put that in the "Find what" field, and put an empty string in the "Replace with" field.
Here's a demo.
Ctrl+H
Find what: ^(?:(.)\1\1|...(.)\2\2).*\R
Replace with: LEAVE EMPTY
UNcheck Match case
check Wrap around
check Regular expression
DO NOT CHECK . matches newline
Replace all
Explanation:
^ : beginning of line
(?: : start non capture group
(.) : group 1, any character but newline
\1\1 : same as group 1, twice
| : OR
... : 3 any character
(.) : group 2, any character but newline
\2\2 : same as group 2, twice
) : end group
.* : 0 or more any character
\R : any kind of linebreak
Result for given example:
AacAap
You can use this pattern:
^(?:...)?(.)\1\1.*\r?\n?
The part (.)\1\1 matches three consecutive same characters with a capture and two backreferences. (?:...)? makes the three first characters optional, this way the consecutive characters can be at the beginning of the line or at the 4th position.
.*\r?\n? is only here to match all remaining characters of the line including the line break (you can preserve line breaks if you want, you only have to remove \r?\n?).
Check on the next regex (?im)^(?:...)?(.)\1\1.*(?:\R|\z).
To try the regex online and get an explanation, please click here.

Replace all newlines between "two lines with certain patterns" with a comma in vim

If I have to replace newlines with comma for all lines between Pattern 1 and Pattern 2, how do I do it?
From:
Pattern 1
abcd
edfgads asd
adsad
...
Pattern 2
to:
Pattern 1, abcd, edfgads asd, adsad, ..., Pattern 2
Use Pattern 1 and Pattern 2 as addresses, see :help cmdline-ranges:
:/^Pattern 1/,/^Pattern 2/-1 s/\n/, /
You can put line number while substituting.
:{pattern1LineNo},{pattern2LineNo}s/\n/, /g
How about
:%s/Pattern 1\_.\{-}Pattern 2/\=join(split(submatch(0), "\n"), ", ")/g
Search
Pattern 1 # obvious
\_. # any character including newline
\{-} # repeat non-greedily (vim's way of writing *?)
Pattern 2 # obvious
The replace part should be clear without an explanation.
For vim it would be
:%s/\n/, /g
You search for a newline character: \n and replace it with comma and space: , which is done globally g, these options are split by / character.
More info about replace in vim you can find here
:g/Pattern1/norm V/Pattern2^MgJ
:g/ on lines matching Pattern1, run the normal mode keystrokes:
visual select as far as... /search for Pattern2
gJ Join selected lines, without adding spaces
NB. Type the ^M with Ctrl-V <Enter>, or Ctrl-Q <Enter>