Regex is finding match but not counting if only matches once - regex

I've written a little piece of code where the user will write a word or sentence in a textbox, click a button and the text in a rich text box is searched, deleting the searched for text if found. That works.
What should also happen is that a message box will pop up saying "Phrase has been deleted X times." The issue I'm having is that this only works if it's found more than once. If it's found once, my programme seems to view it as being found 0 times.
The code is below, any help would be much appreciated -
Try
Dim Selection As New Regex(TextBox1.Text)
Dim deletion As New Regex("\n" & Selection.ToString & "\n")
Dim Stripped As String = Regex.Replace(RichTextBox1.Text, deletion.ToString, vbCr, RegexOptions.Multiline)
RichTextBox1.Clear()
RichTextBox1.AppendText(Stripped)
Dim matchcount = deletion.Matches(RichTextBox1.Text).Count
If matchcount > 0 Then
MsgBox("'" & Selection.ToString & "'" & " has been deleted " & matchcount.ToString & " times.")
Else
MsgBox("'" & Selection.ToString & "'" & " has not been found on a line on its own.")
End If

This is because you are searching \n something \n. I.e., you are looking for two vbLf, but the fist line will not be preceded by a linefeed and the lines might be separated by vbCrLf. You can match the beginning of line with ^ and the end of line with $.
Also, I'm not sure what your intention is. Since RichTextBox1 contains the stripped text, i.e., the text not containing the search pattern anymore, how can you find this text in it with deletion.Matches(RichTextBox1.Text)?
You are declaring a Regex named Selection but you are never using it as such. Instead you are extracting the pattern with Selection.ToString, which is of course exactly the same as the one you entered as TextBox1.Text.
You should count the matches before doing the replace. Also, you must use the same options for counting and replacing
Dim deletion As New Regex("^" & TextBox1.Text & "$", RegexOptions.Multiline)
Dim matchcount = deletion.Matches(RichTextBox1.Text).Count
RichTextBox1.Text = deletion.Replace(RichTextBox1.Text, vbCrLf)
If TextBox1.Text is supposed to contain a regex pattern, then creating the Regex object like this is okay; however, if it is supposed to contain plain text, then you must escape it.
Dim deletion As New Regex("^" & Regex.Escape(TextBox1.Text) & "$",
RegexOptions.Multiline)
Otherwise special regex characters will do their magic instead of being treated as plain text. Example
Regex.Escape("This is a sentence.") ---> "This is a sentence\."

Related

Using regex to find paragraphs in VBA Excel

I am trying to use regex to 'extract' paragraphs in a document. Each paragraph is preceded and followed by a '-' on separate line and each paragraph starts with a number.
For example
-
1. This is a paragraph
It may go over multiple lines
-
Ideally, I would like to not include the '-', but it doesn't really matter as I will be placing it in a string and running another regex against it (One that I know works)
The code I am trying to use is basically as follows
Dim matchPara as Object
Dim regex as Object
Dim theMatch as Object
Dim matches as Object
Dim fileName as String
Dim fileNo as Integer
Dim document as String
matchPara = "-?(\d.*?)?-"
Set regex = CreateObject("VBScript.RegExp")
regex.Pattern = matchPara
regex.Global = True
regex.Multiline = True
fileName = "C:\file.txt"
fileNo = FreeFile
Open fileName For Input As #fileNo
document = Input$(LOF(fileNo), fileNo)
set matches = regex.Execute(document)
For Each theMatch in matches
MsgBox(theMatch.Value)
Next theMatch
Close #fileNo
I have tested this regex on regex101 and it appeared to do what I wanted. I have also tested it without the grouping
-?\d.*?-
However when I run the code the theMatch.Value only ever contains a single '-'. After some messing around with the regex I got it to display the first line of text but never any more than the first line.
I have checked the length of theMatch.Value with:
MsgBox(len(theMatch.Value))
and placed the contents of theMatch.Value in a cell on the worksheet to see if It was cutting off in the message box, but both theories were proved wrong.
I am at a complete loss now and I am beginning to suspect it is possibly a VBA thing and not a regex thing. There is no requirement to use regex, I just assumed it would be the easiest thing to do.
The paragraphs contain data that I am trying to extract. So the idea was to regex each paragraph out place that in a string then run other regex to get the information that I need. Some paragraphs wont contain the data that I need so the idea was to loop through each individual paragraph and then error handle better if the data I need wasn't in that paragraph (ie get what I can and drop the rest with an error message)
Here is a screenshot:
This simple approach does not use Regex. It assumes the data is in column A and the paragraphs are placed in column B:
Sub paragraph_no_regex()
Dim s As String
Dim ary
With Application.WorksheetFunction
s = .TextJoin(" ", False, Columns(1).SpecialCells(2))
End With
ary = Split(s, "-")
i = 1
For Each a In ary
Cells(i, 2) = a
i = i + 1
Next a
End Sub
Sub F()
Dim re As New RegExp
Dim sMatch As String
Dim document As String
re.Pattern = "-\n((.|\n)+?)\n-"
'Getting document
document = ...
sMatch = re.Execute(document)(0).SubMatches(0)
End Sub
If you need dashes -, then just include them into capture group (the outer parenthesis).
This RegEx matches your description and successfully extracts paragraphs (as tested on regex101.com):
matchPara = "-\n\d+\.\s*((?:.|\n)+?)\s*\n-"
It needs the 'global' flag but not the 'multiline' flag. Instead, the end-of-line token is matched in the regex. The main point is that the innermost matching group will match any character including the end-of-line (given as an alternative) but does so in a non-greedy way ("+?"). It doesn't care about word boundaries as this is not necessary here. Also, "-" is not a special character where used in the regex so it doesn't have to be escaped.
As added benefit leading and trailing whitespace is cut off ("\s*" outside the group).

VBA Word Wildcards - finding shortest possible set of characters

I have trouble finding working solution for couple of hours now. I hope you will help me.
My problem:
I need to find and select in Word a whole sentence after providing the starting and ending strings of particular sentence.
For example, when my starting string is "People" and ending string is "apples." I expect Word to select the whole "People like red apples." sentence in my document. (If such a sentence exists)
For this purpose I prepared a macro which works almost like I want. The only problem is that it doesn't select the smallest possible set of characters (which I want it to do). To make it clear let's assume I have this text in my document: People like smoking. People like red apples.
Now, when I provide the starting and ending strings to the macro respectively as "People" and "apples.", it selects all the text, which contains 2 sentences mentioned above. That is my problem: I wanted it to select only the second sentence (People like red apples.), not both of them, even though they start with the same word. So, basically, I always want to select the shortest possible set of characters (which in this case is only the last sentence).
Here is a part of my macro in VBA:
`text_str = startStr & "*" & endStr
With Application.Selection.Find
.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.Text = text_str
.MatchWildcards = True
.MatchCase = True
.Execute
End With
I know the problem is with the Wildcards (or very limited set of regular expressions), so I also tried something like this as the search string:
text_str = "(" & startStr & "*){1}" & endStr
It also didn't help. I'm stuck here. :/
Thanks for any suggestions!
Selection.Find has something similar to regular expressions,
but in this case you must use real regular expressions.
The pattern (in this particular case) should be:
People[^.]+apples\.
I wrote an example macro, which:
Selects the whole text in the document and assigns it to src
variable (searched by the regex).
Sets the cursor at the beginning of the document.
Checks whether the pattern can be matched (regEx.Test).
Executes the regex.
Assigns the matched string to ret variable.
Displays it in a message box.
Below you have a complete macro. Probably you should change it to
select (find) the text matched (instead of the message box).
Sub Re()
Dim startStr As String: startStr = "People"
Dim endStr As String: endStr = "apples"
Dim pattern As String: pattern = startStr & "[^.]+" & endStr & "\."
Dim regEx As New RegExp
Dim src As String
Dim ret As String
Dim colMatches As MatchCollection
ActiveDocument.Range.Select
src = ActiveDocument.Range.Text
Selection.StartOf
regEx.pattern = pattern
If (regEx.Test(src)) Then
Set colMatches = regEx.Execute(src)
ret = "Match: " & colMatches(0).Value
Else
ret = "Matching Failed"
End If
MsgBox ret, vbOKOnly, "Result"
End Sub

Extract text using word VBA regex then save to a variable as string

I am trying to create code in Word VBA that will automatically save (as PDF) and name a document based on it's content, which is in text and not fields. Luckily the formatting is standardized and I already know how to save it. I tested my regex elsewhere to make sure it pulls what I am looking for. The trouble is I need to extract the matched statement, convert it to a string, and save it to an object (so I have something to pass on to the code where it names the document).
The part of the document I need to match is below, from the start of "Program" through the end of the line and looks like:
Program: Program Name (abr)
and the regex I worked out for this is "Program:[^\n]"
The code I have so far is below, but I don't know how to execute the regex in the active document, convert the output to a string and save to an object:
Sub RegExProgram()
Dim regEx
Dim pattern As String
Set regEx = CreateObject("VBScript.RegExp")
regEx.IgnoreCase = True
regEx.Global = False
regEx.pattern = "Program\:[^\n]"
(missing code here)
End Sub
Any ideas are welcome, and I am sorry if this is simple and I am just overlooking something obvious. This is my first VBA project, and most of the resources I can find suggest replacing using regex, not saving extracted text as string. Thank you!
Try this:
You can find documentation for the RegExp class here.
Dim regEx as Object
Dim matchCollection As Object
Dim extractedString As String
Set regEx = CreateObject("VBScript.RegExp")
With regEx
.IgnoreCase = True
.Global = False ' Only look for 1 match; False is actually the default.
.Pattern = "Program: ([^\r]+)" ' Word separates lines with CR (\r)
End With
' Pass the text of your document as the text to search through to regEx.Execute().
' For a quick test of this statement, pass "Program: Program Name (abr)"
set matchCollection = regEx.Execute(ActiveDocument.Content.Text)
' Extract the first submatch's (capture group's) value -
' e.g., "Program Name (abr)" - and assign it to variable extractedString.
extractedString = matchCollection(0).SubMatches(0)
I've modified your regex based on the assumption that you want to capture everything after Program: through the end of the line; your original regex would only have captured Program:<space>.
Enclosing [^\r]+ (all chars. through the end of the line) in (...) defines a so-called subexpression (a.k.a. capture group), which allows selective extraction of only the substring of interest from what the overall pattern captures.
The .Execute() method, to which you pass the string to search in, always returns a collection of matches (Match objects).
Since the .Global property is set to False in your code, the output collection has (at most) 1 entry (at index 0) in this case.
If the regular expression has subexpressions (1 in our case), then each entry of the match collection has a nonempty .SubMatches collection, with one entry for each subexpression, but note that the .SubMatches entries are strings, not Match objects.
Match objects have properties .FirstIndex, .Length, and Value (the captured string). Since the .Value property is the default property, it is sufficient to access the object itself, without needing to reference the .Value property (e.g., instead of the more verbose matchCollection(0).Value to access the captured string (in full), you can use shortcut matchCollection(0) (again, by contrast, .SubMatches entries are strings only).
If you're just looking for a string that starts with "Program:" and want to go to the end of the line from there, you don't need a regular expression:
Public Sub ReadDocument()
Dim aLine As Paragraph
Dim aLineText As String
Dim start As Long
For Each aLine In ActiveDocument.Paragraphs
aLineText = aLine.Range.Text
start = InStr(aLineText, "Program:")
If start > 0 Then
my_str = Mid(aLineText, start)
End If
Next aLine
End Sub
This reads through the document line by line, and stores your match in the variable "my_str" when it encounters a line that has the match.
Lazier version:
a = Split(ActiveDocument.Range.Text, "Program:")
If UBound(a) > 0 Then
extractedString = Trim(Split(a(1), vbCr)(0))
End If
If I remember correctly, paragraphs in Word end with vbCr ( \r not \n )

Remove all comments from a PHP source file

I would like to remove all comments from a PHP source file from within a VB.NET application. Another stackoverflow question showed how to do this in C# code
I came up with this conversion, but it does not work unfortunately:
Dim blockComments As String = "/\*(.*?)\*/"
Dim lineComments As String = "//(.*?)\r?\n"
Dim strings As String = """((\\[^\n]|[^""\n])*)"""
Dim verbatimStrings As String = "#(""[^""]*"")+"
regex = New Regex(blockComments & "|" & lineComments)
srcT = regex.Replace(srcT, "")
You need to pass the flag RegexOptions.Singleline when constructing the Regex object. Otherwise, the block-comments can't span multiple lines.
regex = New Regex(blockComments & "|" & lineComments, RegexOptions.Singleline)
The . normally matches any character except newline (\n). The RegexOptions.Singleline flag makes it match any character, including newline.

Whole word replacements using Regular Expression

I have a list of original words and replace with words which I want to replace occurrence of the original words in some sentences to the replace words.
For example my list:
theabove the above
myaddress my address
So the sentence "This is theabove." will become "This is the above."
I am using Regular Expression in VB like this:
Dim strPattern As String
Dim regex As New RegExp
regex.Global = True
If Not IsEmpty(myReplacementList) Then
For intRow = 0 To UBound(myReplacementList, 2)
strReplaceWith = IIf(IsNull(myReplacementList(COL_REPLACEMENTWORD, intRow)), " ", varReplacements(COL_REPLACEMENTWORD, intRow))
strPattern = "\b" & myReplacementList(COL_ORIGINALWORD, intRow) & "\b"
regex.Pattern = strPattern
TextToCleanUp = regex.Replace(TextToReplace, strReplaceWith)
Next
End If
I loop all entries in my list myReplacementList against the text TextToReplace I want to process, and the replacement have to be whole word so I used the "\b" token around the original word.
It works well but I have a problem when the original words contain some special characters for example
overla) overlay
I try to escape the ) in the pattern but it does not work:
\boverla\)\\b
I can't replace the sentence "This word is overla) with that word." to "This word is overlay with that word."
Not sure what is missing? Is regular expression the way to the above scenario?
I'd use string.replace().
That way you don't have to escape special chars .. only these: ""!
See here for examples: http://www.dotnetperls.com/replace-vbnet
Regex is good if your looking for patterns. Or renaming your mp3 collection ;-) and much, much more. But in your case, I'd use string.replace().