Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi! I am very new to regex and have been testing for hours to find an expression that works? I work in the VB.NET language, VS2015, Windows Forms, Win10.

I actually found a regex expression that appears to work for what I need on several regex tester sites (i.e. extract the picwgoal and pichgoal values independantly). But, it doesn't seem to be accepted in my project? Intellisense accuses that the ? cannot be used here? Then, the \b is separated like so, \ b... I'm guessing either the REGEX flavour isn't the correct one for VB.NET, or I just don't know how to use the expression?

I am working on a solution which converts RTF content in a RichTextBox to RTF markup code in a separate RTB. It also converts from RTF markup to display the original RTF document.

My goal is to extract both the picwgoal and pichgoal values from the test string below to display on 2 separate labels. Then, using a numeric up/down, resize these values by a percentage to scale the image down to size. Then, I need to replace the original picwgoal and pichgoal values with the new values (i.e. in twips) to resize/scale the image down to the required size. This works manually, but I would like to automate the task!

I have attempted using Regex.Match examples, but the problem seems to be the Regex expression itself? What I think I need to do, but don't know how, is:

- Search for picwgoal.
- Extract the numerical value (i.e. variable) which follows it.
- Scale the value down according to a user input percentage (i.e. using a numerical up/down).
- Replace the original value in the RTF markup with the new scaled down value.
- Repeat the operation for pichgoal (maybe both values can be replaced in a single operation???).

The test string is a partial extract from RTF markup code for an image and text!

If you are able to give me any pointers or help on this I would greatly appreciate it! I plan on posting the solution as soon as I get over the hurdles :-)

Thanks!!!

What I have tried:

' Step 1: create Regex.
Dim regex As Regex = New Regex("\d+")

 Step 2: call Match on Regex.
Dim match As Match = regex.Match(?:picwgoal)(\d+)

' Step 3: test the Success bool.
' ... If we have Success, write the Value.
If match.Success Then
    Console.WriteLine(match.Value)
End If



REGEX EXPRESSION: (?:picwgoal)(\d+)


TEST STRING:

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}\viewkind4\uc1\pard\f0\fs17{\pict\wmetafile8\picw20793\pich13968\picwgoal11788\pichgoal7919 010009000003f88109000000e28109000000050000000b0200000000050000000c0290363951e2
Posted
Updated 9-Jun-20 7:48am
v2
Comments
Member 13032047 8-Jun-20 17:48pm    
I am experimenting with capture groups, i.e. "(picwgoal)(\d+)", which correctly divides the text and numeric value into two groups. A couple of questions: 1. Does this work in VB.NET? 2. How can I extract the second capture group (i.e. numeric value)? The picwgoal and pichgoal texts are constants, but the numeric values that follow them change according to the image from which the RTF code is generated! Therefore, I can't use a comparative string? I need to find the picwgoal and pichgoal text in the RTF markup, then substitute the following numeric value with a new numeric value? Really would appreciate any help on this!!!
Patrice T 8-Jun-20 19:53pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.
Member 13032047 8-Jun-20 18:02pm    
Found a way to ignore the first capture group making the second capture group become Group 1 (https://regex101.com/). So now, instead of listing two groups in the results, there is only one group (i.e. the numeric value I want). Looks good, but still don't know how to use this in code???
Patrice T 9-Jun-20 3:45am    
What about showing your code ?
Member 13032047 9-Jun-20 10:01am    
Thanks Patrice! I updated the code showing one example of several that I've tried. I am a novice at REGEX and am doing the work to try to learn! However, folks mostly post just the regex expression they tested on regex testers without giving an example how to use the expression in actual real world code! This makes it rather difficult to learn! Regex testers test expressions, but don't actually show any code examples. For example, the expression I have now placed in code works on several regex testers, but how do I make it work in actual code???

Quote:
Found a way to ignore the first capture group making the second capture group become Group 1 (https://regex101.com/). So now, instead of listing two groups in the results, there is only one group (i.e. the numeric value I want). Looks good, but still don't know how to use this in code???

Simple: .NET Framework Regular Expressions | Microsoft Docs[^] will show you how.
[Update]
Quote:
What kind of REGEX expression do I need and how do I use it in real world code which doesn't depend on static data???

Find tutorials like: VB.Net - Regular Expressions - Tutorialspoint[^]
 
Share this answer
 
v2
Comments
Member 13032047 9-Jun-20 10:25am    
Thanks OriginalGriff! I took a look at the link you posted! However, I find the same problem with the examples given there as with the many examples I have already studied on other sites all of which generally depend on a "static" test string. The RTF code generated in the program I am creating is "dynamic" in the sense that each image and RTF text inserted into the program is different. Therefore, the RTF Markup generated will be different each time! The picwgoal and pichgoal statements are constants, but the digits (i.e. image width and height) following those statements are variable (and therefore unknown), i.e. a screenshot (bitmap) or an image from a webpage are smaller images and give one kind of result. Photos taken with a digital camera are much larger and the numerical format changes!!! I cannot write code using a regex expression which requires a static number in a test string to find and replace an unknown number??? Doesn't appear to be very simple??? How do I represent unknown digits??? How do I know how many digits will appear in the RTF markup??? What kind of REGEX expression do I need and how do I use it in real world code which doesn't depend on static data???
Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx: Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx: Regexper[^]
 
Share this answer
 
Comments
Member 13032047 9-Jun-20 10:31am    
Hi again Patrice! I greatly appreciate the value of REGEX testers! But, as I have expressed above, they don't give me a real world example of how to use a working expression I have tested? As I mentioned to OriginalGriff, I am working with the unknown and cannot use a static test string to test a variable numeric value??? I tested the regex expression in my code above and on several regex testers it works! But, what kind of code do I need to use to make the expression work in the real world with unknown variable numeric values???
Guys thanks for your help! I played with the code above and finally got a version that works! See below:

Dim pattern As String = "(?:picwgoal)(\d+)"
        Dim pattern2 As String = "(?:pichgoal)(\d+)"
        Dim input As String = RichTextBox2.Text

        Dim rgx As New Regex(pattern)
        Dim rgx2 As New Regex(pattern2)
        Dim groupNumbers() As Integer = rgx.GetGroupNumbers()
        Dim groupNumbers2() As Integer = rgx2.GetGroupNumbers()
        Dim m As Match = rgx.Match(input)
        Dim m2 As Match = rgx2.Match(input)
        If m.Success Then

            Label8.Text = ("Width (picwgoal): " & m.Value.Replace("picwgoal", ""))
            Label9.Text = ("Height (pichgoal): " & m2.Value.Replace("pichgoal", ""))

        End If
 
Share this answer
 
Hey guys, I found some code (i.e. below) that roughly works with the regex expression! But, I'm having a couple of problems: 1. I want to include both picwgoal and pichgoal values in the same code block, but although I tried altering the code variables, I couldn't get this working without repeating the same code block twice with different variables? 2. This code shows me two messages: i.e. picwgoal11788 then 11788 and the same for pichgoal. This is correct behaviour because it is showing messages for the Full Match and capture Group 1. But, I only need one message? How can I achieve this?


'PROJECT: Regex.GetGroupNumbers Method
'SOURCE CODE: https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.getgroupnumbers?view=netcore-3.1
        

        Dim pattern As String = "(?:picwgoal)(\d+)"
        Dim input As String = RichTextBox2.Text

        Dim rgx As New Regex(pattern)
        Dim groupNumbers() As Integer = rgx.GetGroupNumbers()
        Dim m As Match = rgx.Match(input)
        If m.Success Then
            Console.WriteLine("Match: {0}", m.Value)
            For Each groupNumber In groupNumbers
                Dim name As String = rgx.GroupNameFromNumber(groupNumber)
                Dim number As Integer
                Console.WriteLine("   Group {0}{1}: '{2}'",
                              groupNumber,
                              If(Not String.IsNullOrEmpty(name) And
                              Not Int32.TryParse(name, number),
                                 " (" + name + ")", String.Empty),
                              m.Groups(groupNumber).Value)

                MsgBox(m.Groups(groupNumber).Value)
            Next
        End If


The Console results were:

Match: picwgoal11788
Group 0: 'picwgoal11788'
Group 1: '11788'
Match: pichgoal7919
Group 0: 'pichgoal7919'
Group 1: '7919'
 
Share this answer
 
v3
Comments
CHill60 9-Jun-20 11:19am    
You should use the "Improve Question" link to add information to your post. This is a question not a solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900