Find line + 1 line after it - regex

How to find line + one line after that?
Eg
I have the lines
123123
some text
546454
some text
354543
some text
465466
some text
123123
some text
So i know how to find 123123 (^123123) and other but how get +1 line with text 'some text'
result shoud be
123123
some text
123123
some text
Update
I forgot to say, after text can be other text 123123 is start of line
123123 text text text text text
some text
Sorry

You can use a new line between your pattern and related regex for next line :
/123123\n[^\d]+/g
Demo https://regex101.com/r/zA2cU6/1
Note that [^\d]+ will match any combination of characters which doesn't contains digit.(you can change it based on your need)
Update:
If you want to match the text after your pattern you just can add .* after it :
123123.*\n[^\d]+
Demo : https://regex101.com/r/zA2cU6/2

Related

Regular expression to match a string into a text

I've a TXT file and I use a Reg Expr to get the text after a string
"Diagnosis Statement:"
the Full text is :
Diagnosis Statement:
6/28/2011
RZZZCG77T77G355S
Report text is here ....... end of report
I would get only the 16 digits "RZZZCG77T77G355S" into the text
(16 digits mix of numbers and capital letters)
I can get the text after the string "Diagnosis Statement:" with :
(?ms)^Diagnosis Statement\s*:(?<value>.*)
and I get the code with :
^[A-Z0-9]{16}?$
But cannot get the correct way to merge both and get only the 16 digits string from
text after "Diagnosis Statement:"
Can you give some help ?
On your example, ^([A-Z0-9]{16})$ is enough. Test it here.
Do you have another place in the document where you find text with a format of 16 char, with only captials and digits ? If no, this regex is enough.
With the following regex, you can find the first thing you find with the good format, following "Diagnosis Statement:" and one line of random text.
Diagnosis Statement:\s*.*\s*\K^([A-Z0-9]{16})$
Test it here

How can I ignore a bunch of words that might have in a text or not, with regex

I have the following possible texts:
Any text here!
Any foo text here!
Any text foo here!
Any text here foo!
And want my match to be always:
Any text here!
I've already tried the pattern /(.+)(?!foo\s)?(.+)/g printing the two capturing groups ($1$2) but didn't succeed as the .* takes the foo part.
First question is if is it possible and seccond would be how?
Try the following:
(?(?=.*foo)(.+)(?:\sfoo)(.+)|(.+))
group1 and group2 ( ($1$2) ) gives Any text here! from line 2 - line 4
group3 gives Any text here! from line 1
1 Any text here!
2 Any foo text here!
3 Any text foo here!
4 Any text here foo!
demo at regex101

Regex - Adding a new line and word after every line beginning with certain phrase

I'm currently writing a program in java, and I have 100s of different lines of code beginning with
Bw.write
How can I make it so after every line beginning with Bw.write, I can insert a new line under it with BufferedWriter.newLine();
Thanks in advance
EDIT: Sorry for the confusion
For example I need
Bw.write 12345
Bw.write 98765
to become
Bw.write 12345
BufferedWriter.newLine();
Bw.write 98765
BufferedWriter.newLine();
Replace Bw\.write.* with $0\r\nBufferedWriter\.newLine()
Click for DEMO
Before Replace:
After Replace All:

Is the VBScript RegEx Flavor Lookaround Method known to have problems with textfiles exceeding 5MB?

I would like to know why the following RegEx's:
\b\w{7}\b\s[1]\s[\S\s]+?(?=WHAT WHERE WHAT WHERE WHAT\,\sWHERE\sWHAT.)
and:
\b\w{7}\b\s[1]\s[\S\s]+?(?=WHAT WHERE WHAT WHERE WHAT\,\sWHERE\sWHAT.|HOW WHO HOW WHO HOW\,\sWHO\sHOW\.)
seem to work perfectly fine on the following test string:
THIS THAT THIS THAT THIS,
THAT
THIS.
CHAPTER 1
Text text text 2 text text text 3 text text text 4 text text text.
CHAPTER 2
Text text text 2 text text text 3 text text text 4 text text text.
CHAPTER 3
Text text text 2 text text text 3 text text text 4 text text text.
WHAT WHERE WHAT WHERE WHAT,
WHERE
WHAT.
CHAPTER 1
Text text text 2 text text text 3 text text text 4 text text text.
CHAPTER 2
Text text text 2 text text text 3 text text text 4 text text text.
CHAPTER 3
Text text text 2 text text text 3 text text text 4 text text text.
HOW WHO HOW WHO HOW,
WHO
HOW.
CHAPTER 1
Text text text 2 text text text 3 text text text 4 text text text.
CHAPTER 2
Text text text 2 text text text 3 text text text 4 text text text.
CHAPTER 3
Text text text 2 text text text 3 text text text 4 text text text.
IF OR IF OR IF.
CHAPTER 1
Text text text 2 text text text 3 text text text 4 text text text.
CHAPTER 2
Text text text 2 text text text 3 text text text 4 text text text.
CHAPTER 3
Text text text 2 text text text 3 text text text 4 text text text.
TO FOR TO FOR
TO FOR TO FOR.
CHAPTER 1
Text text text 2 text text text 3 text text text 4 text text text.
CHAPTER 2
Text text text 2 text text text 3 text text text 4 text text text.
CHAPTER 3
Text text text 2 text text text 3 text text text 4 text text text.
IN UNDER IN
UNDER IN UNDER.
CHAPTER 1
Text text text 2 text text text 3 text text text 4 text text text.
CHAPTER 2
Text text text 2 text text text 3 text text text 4 text text text.
CHAPTER 3
Text text text 2 text text text 3 text text text 4 text text text.
LEFT RIGHT LEFT
RIGHT LEFT.
CHAPTER 1
Text text text 2 text text text 3 text text text 4 text text text.
CHAPTER 2
Text text text 2 text text text 3 text text text 4 text text text.
CHAPTER 3
Text text text 2 text text text 3 text text text 4 text text text.
UP DOWN UP DOWN UP
DOWN.
CHAPTER 1
Text text text 2 text text text 3 text text text 4 text text text.
CHAPTER 2
Text text text 2 text text text 3 text text text 4 text text text.
CHAPTER 3
Text text text 2 text text text 3 text text text 4 text text text.
THE END.
But, when I use the same type of Expression on files that exceed 5MB, it fails.
The VBScript that I am using is as follows:
Option Explicit
Dim strPath : strPath = "myFile.txt"
If Instr(1, WScript.FullName, "CScript", vbTextCompare) = 0 Then
With CreateObject("WScript.Shell")
.Run "cmd.exe /k cscript //nologo """ & WScript.ScriptFullName & """", 1, False
WScript.Quit
End With
Else
With CreateObject("Scripting.FileSystemObject")
If .FileExists(strPath) Then
Call Main(strPath)
Else
WScript.Echo "Input file doesn't exists"
End If
End With
End If
Private Sub Main(filePath)
Dim TempDictionary, Books, Book, b
Set TempDictionary = CreateObject("Scripting.Dictionary")
Set Books = RegEx(GetFileContent(filePath),"\b\w{7}\b\s[1]\s[\S\s]+?THE SECOND BOOK OF MOSES")
If Books.Count > 0 Then
For Each Book In Books
WScript.Echo Replace(Left(Book.Value,70),vbCrLf," ")
Next
Else
WScript.Echo "Document didn't contain any valid books"
WScript.Quit
End If
End Sub
Private Function GetFileContent(filePath)
Dim objFS, objFile, objTS
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objFile = objFS.GetFile(filePath)
Set objTS = objFile.OpenAsTextStream(1, 0)
GetFileContent = objTS.Read(objFile.Size)
Set objTS = Nothing
End Function
Private Function RegEx(str,pattern)
Dim objRE, Match, Matches
Set objRE = New RegExp
objRE.Pattern = pattern
objRE.Global = True
Set RegEx = objRE.Execute(str)
WScript.Echo objRE.Test(str)
End Function
Editor that I am using is here: http://www.regexr.com/
Q: What are you trying to do?
A: I want to be able to split any textfile into several string chunks, based on a smart regex code that captures anything between two strings. The first string determiner is a fixed term, i.e. "CHAPTER 1", but the second string determiner is unfixed. The second string determiner is unfixed and changing, but it is known. It can be placed into an array, and then parsed.
The problem that I am having is that the Lookaround (?=) seems to either escape or get stuck in a loop. I have been playing around with the "|" operator, as you can see in the second RegEx at the start of this OP.
The test file that I am working with seems to parse just fine. No problem. But the larger files that I am working with... I don't know. Something just goes wrong.

Notepad ++ clone each line

I have a file that includes such lines
111
112
113
I want to clone the lines and add a seperator between the numbers. Output should be as the following
111#111
112#112
113#113
How can I do it with notepadd++ by using regex replace
Find (.+)
Replace \1#\1
This will work