Move all text between two "\t" in Notepad++ - regex

I have more than a million lines of text in this format:
AAAA BBBBBBBBBBBBBBB CCCC
Separated by \t
I want to have it in a format
AAAA_CCCC BBBBBBBBBBBBBBB
But I cannot seem to figure out how to do it using regular expressions in Notepad++

You may try the following find and replace, in regex mode:
Find: ^(\S+)\t(\S+)\t(\S+)$
Replace: $1_$3 $2
Here is a demo.

If the separator is a tab, you can use
^[^\r\n\t]+\K\t([^\r\n\t]+)\t([^\r\n\t]+)$
The pattern matches:
^ Start of string
[^\r\n\t]+ Match 1+ chars other than a tab or newline
\K\t Forget what is matches so far using \K and match a tab
([^\r\n\t]+) Capture group 1, match any 1+ chars other than a newline or tab
\t Match a tab
([^\r\n\t]) Capture group 2, match 1 char other than a newline or tab
$ end of string
In the replacement use the 2 capture groups with an underscore in between.
_$2 $1
See a regex demo.
The result of the replacement:
AAAA_CCCC BBBBBBBBBBBBBBB

Related

Match everything except a selection of words starting from word1 and ending at word2

I am trying to match 'match me please' within the following String in Python flavor:
bar\nStartOfString\nignoreMe\ntoBeIgnored\nmatch me please\nignoreYou\nEndOfString\nfoo
The result has to exclude ignoreMe, toBeIgnored and ignoreYou as the position of these words is relative.
What I've tried so far:
StartOfString((?!ignoreMe)(?!toBeIgnored)(?!ignoreYou).*)EndOfString
\b(?!ignoreMe|ignoreYou|toBeIgnored)\b\S+
https://regex101.com/r/VaROsW/2
Can anyone please help?
You could use a capture group to capture what you want.
If you want to match \n (backslash and an n char), you can match as least amount of characters between the words in the alternation.
(?:(?:ignore(?:Me|You)|toBeIgnored)\\n)+(.*?)\\n(?:ignore(?:Me|You)|toBeIgnored)
Regex demo
If \n is a newline, you can prevent matching the words in the alternation using a negative lookahead:
^(?:ignore(?:Me|You)|toBeIgnored)((?:\n(?!ignore(?:Me|You)|toBeIgnored).*))\n(?:ignore(?:Me|You)|toBeIgnored)
Regex demo

Using regex to duplicate a selection and replacing some characters

Probably a terrible title.
I am trying to take the following:
Joe Dane
Bob Sagget
Whitney Houston
Some
Other
Test
And trying to produce:
JOE_DANE("Joe Dane"),
BOB_SAGGET("Bob Sagget"),
WHITNEY_HOUSTON("Whitney Houston"),
SOME("Some"),
OTHER("Other"),
TEST("Test"),
I'm using Notepad++ and am close but not good enough at regex to figure out the remaining expression. So far, this is what I have:
Find what: (^.*)
Replace with: \1 \(\"\1\"\),
Produces: Joe Dane("Joe Dane"),
I've tried replacing with: \U$1 \(\"\1\"\), but this also impacts the second instance of \1 with upper case. It also does not replace the whitespace with an underscore _.
This can be done in a single step.
If you don't have more than 2 words in a line:
Ctrl+H
Find what: ^(\S+)(?: (\S+))?$
Replace with: \U$1(?2_$2)\E\("$0"\),
CHECK Wrap around
CHECK Regular expression
Replace all
Explanation:
^ # beginning of line
(\S+) # group 1, 1 or more non space
(?: (\S+))? # non capture group, a space, group 2, 1 or more non space, optional
$
Replacement:
\U # uppercased
$1 # group 1
(?2_$2) # if group 2 exists, add and underscore before
\E # end uppercase
\("$0"\), # the whole match with parens and quote
Screenshot (after):
If you have more than 2 words (up to 5), use:
Find ^(\S+)(?: (\S+))?(?: (\S+))?(?: (\S+))?(?: (\S+))?
Replace: \U$1(?2_$2)(?3_$3)(?4_$4)(?5_$5)\E\("$0"\),
I you have more thans five word, add as many (?: (\S+))? as needed.
You might do it in 2 steps, first matching any char 1+ more times from the start of the string.
Find what
^.+
For the first replacement you can use \E to end the activation of \U and use the full match $0
Replace with
\U$0\E\("$0"\),
For the second step, to replace the spaces with underscores, you could skip over the text between parenthesis, and match spaces between uppercase chars.
Find what
\(".*?"\)(*SKIP)(*F)|[A-Z]+\K\h+(?=[A-Z])
\(".*?"\) Match from (" till ")
(*SKIP)(*F)| Skip this part of the match
[A-Z]+\K Match uppercase chars and use \K to clear the current match buffer (forget what is matches do far)
\h+(?=[A-Z]) Match 1+ horizontal whitespace chars and assert an uppercase char to the right
Replace with _

How to extract all the strings between 2 patterns using regex Notepad++?

Extract all the string between 2 patterns:
Input:
test.output0 testx.output1 output3 testds.output2(\t)
Output:
output0 output1 ouput3 output2
Note: (" ") is the tab character.
You may try:
\.\w+$
Explanation of the above regex:
\. - Matches . literally. If you do not want . to be included in your pattern; please use (?<=\.) or simply remove ..
\w+ - Matches word character [A-Za-z0-9_] 1 or more time.
$ - Represents end of the line.
You can find the demo of the regex in here.
Result Snap:
EDIT 2 by OP:
According to your latest edit; this might be helpful.
.*?\.?(\w+)(?=\t)
Explanation:
.*? - Match everything other than new line lazily.
\.? - Matches . literally zero or one time.
(\w+) - Represents a capturing group matching the word-characters one or more times.
(?=\t) - Represents a positive look-ahead matching tab.
$1 - For the replacement part $1 represents the captured group and a white-space to separate the output as desired by you. Or if you want to restore tab then use the replacement $1\t.
Please find the demo of the above regex in here.
Result Snap 2:
Try matching on the following pattern:
Find: (?<![^.\s])\w+(?!\S)
Here is an explanation of the above pattern:
(?<![^.\s]) assert that what precedes is either dot, whitespace, or the start of the input
\w+ match a word
(?!\S) assert that what follows is either whitespace of the end of the input
Demo

How to write nested regex to find words below some string?

I am converting one pdf to text with xpdf and then find some words
with help of regex and preg_match_all.
I am seperating my words with colon in pdftotext.
Below is my pdftotext output:
In respect of Shareholders
Name: xyx
Residential address: dublin
No of Shares: 2
Name: abc
Residential address: canada
No of Shares: 2
So i write one regex that will show me words after colon in text().
$regex = '/(?<=: ).+/';
preg_match_all($regex, $string, $matches);
But Now i want regex that will display all data after In respect of Shareholders.
So, i write $regex = '/(?<=In respect of Shareholders).*?(?=\s)';
But it shows me only :
Name: xyx
I want first to find all data after In respect of shareholders and then another regex to find words after colon.
You may use
if (preg_match_all('~(?:\G(?!\A)|In respect of Shareholders)\s*[^:\r\n]+:\h*\K.*~', $string, $matches)) {
print_r($matches[0]);
}
See the regex demo
Details
(?:\G(?!\A)|In respect of Shareholders) - either the end of the previous successful match or In respect of Shareholders text
\s* - 0+ whitespaces
[^:\n\r]+ - 1 or more chars other than :, CR and LF
: - a colon
\h* - 0+ horizontal whitespaces
\K - match reset operator that discards all text matched so far
.* - the rest of the line (0 or more chars other than line break chars).
In your regex (?<=: ).+ you will match any character 1+ times after a colon and a space. To capture all that follows the spaces or tabs in a group, you could use (?<=: )[\t ](.+)
Another way to match the texts using a capturing group could be:
^.*?:[ \t]+(\w+)
Explanation
^ Assert start of the string
.*?: Match any character non greedy followed by a :
[ \t]+ Match 1+ times a space or a tab
(\w+) Capture in a group 1+ word characters
Regex demo | Php demo
Or use \K to forget what was matched if that is supported:
^.*?:\h*\K\w+
Regex demo

How replace last instance of space with comma

I have a text file and I would like to replace the last space with a comma to facilitate data import and processing.
My text file has the following sample lines:
Some text 123 here and then 44.99
more text 789 is 33.75
The result I'd like to obtain:
Some text 123 here and then,44.99
more text 789 is,33.75
You can use the following regex replacement:
Find what: \h+(\S+)$
Replace with: ,\1
See the regex demo.
Details
\h+ - 1 or more (+) repetitions of any horizontal whitespaces (\h)
(\S+) - Capturing group 1: any one or more chars other than whitespace (\S)
$ - end of a line.
The ,\1 replacement replaces the matched text with a comma and the contents of Group 1.