none
Regex question RRS feed

  • Question

  • I have the following line of a string:

    37. Qh6 {[%eval 1643,29] 2.6 } 37... Bf7 {[%eval -1040,29][%emt 0:00:06]}

    Dim pattern1 AsString= "\d+\. "

    What I want it to do is take out the 37. which it does.

    Qh6 {[%eval 1643,29] 2.6 } 37... Bf7 {[%eval -1040,29][%emt 0:00:06]}

    The problem is it is also taking out the 2.6 which I don't want it to be touched.  Is there a way of mandating in the regex that requires not just a digit and period has to exist but that the next term after the decimal can NOT be another digit??. That would hopefully take care of my problem.

    Hopefully I a made myself clear.

    Thanks Les

    Sunday, May 5, 2019 8:50 PM

Answers

  • Try this pattern too:

       \d+\.(?!\d)

    • Marked as answer by Les2011 Monday, May 6, 2019 7:54 PM
    Monday, May 6, 2019 3:40 AM
  • Hi

    It can be difficult to understand fully what is needed (when it comes to space characters),

    Here is Viorel pattern, with an additional space character in the pattern following the \d+\. sequence - see if that is what is needed.

            Dim s = "37. Qh6 {[%eval 1643,29] 2.6} 38. Bf7 {[%eval -1040,29] [%emt 0:00:06]} 39. Qh7 {[%eval 1643,32] [%emt 0:00:06]}"
    
            ' viorel pattern with additional space
            ' after \d+\.
            s = Regex.Replace(s, "\d+\. (?!\d)", String.Empty)
    
            ' RESULT
            ' Qh6 {[%eval 1643,29] 2.6} Bf7 {[%eval -1040,29] [%emt 0:00:06]} Qh7 {[%eval 1643,32] [%emt 0:00:06]}


    Regards Les, Livingston, Scotland

    • Marked as answer by Les2011 Monday, May 6, 2019 4:56 PM
    Monday, May 6, 2019 12:55 PM

All replies

  • Hi,

    Are you trying to take the number between "37." and the decimal?You can try the code:

    Imports System.Text.RegularExpressions
    Public Class Form1
        Dim pattern1 As String = "37. Qh6 {[%eval 1643,29] 2.6 } 37... Bf7 {[%eval -1040,29][%emt 0:00:06]}"
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            MsgBox(Regex.Match(pattern1, "(?<=37.).*?(?=[0-9]{1,}[.][0-9])").Value)
        End Sub
    End Class
    

    result: Qh6 {[%eval 1643,29] 

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 6, 2019 2:32 AM
  • Try this pattern too:

       \d+\.(?!\d)

    • Marked as answer by Les2011 Monday, May 6, 2019 7:54 PM
    Monday, May 6, 2019 3:40 AM
  • Hi Alex,

    First let me thank you for trying to help.  I think perhaps I need to go further into an explanation as to what I am trying to achieve.  BTW the regex I currently have has always done what I wanted it to do but when it came across that 2.6 number things got screwy.  Let me show you another string.

    string = "37. Qh6 {[%eval 1643,29] 2.6} 38. Bf7 {[%eval -1040,29] [%emt 0:00:06]} 39. Qh7 {[%eval 1643,32] [%emt 0:00:06]}"

    The numbers 37.   38.   39. represent move numbers in the game of chess but that's not important.  The regex expression I work with is  "\d+\. " however it also removes the 2.6.  This is the problem and what I have been doing is any move number (ie 37.) I set to blank throughout the entire string so nothing else changes except making all the move numbers blank.  Apparently the regex expression I show above also hits on the 2.6.  Is there a way that I can eliminate it from the regex expression in the following way.  if the number is followed by a "." and a blank space set it to blank but if the decimal is not followed by a blank space leave that part of the string alone?  I guess I am trying to develop some kind of a rule that only make the changes in a string that is a number followed by a period and a blank space, anything else don't touch it.

    So the final string will look like:

    string = "Qh6 {[%eval 1643,29] 2.6} Bf7 {[%eval -1040,29] [%emt 0:00:06]} Qh7 {[%eval 1643,32] [%emt 0:00:06]}"
    

    Thanks for your help

    Les

    Monday, May 6, 2019 5:35 AM
  • Hi,

    try the code:

    Regex.Match(pattern1, "(?<=37.).*?(?=38.)").Value & Regex.Match(pattern1, "(?<=38.).*?(?=39.)").Value & Regex.Match(pattern1, "(?<=39.).*").Value

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 6, 2019 6:43 AM
  • Hi

    It can be difficult to understand fully what is needed (when it comes to space characters),

    Here is Viorel pattern, with an additional space character in the pattern following the \d+\. sequence - see if that is what is needed.

            Dim s = "37. Qh6 {[%eval 1643,29] 2.6} 38. Bf7 {[%eval -1040,29] [%emt 0:00:06]} 39. Qh7 {[%eval 1643,32] [%emt 0:00:06]}"
    
            ' viorel pattern with additional space
            ' after \d+\.
            s = Regex.Replace(s, "\d+\. (?!\d)", String.Empty)
    
            ' RESULT
            ' Qh6 {[%eval 1643,29] 2.6} Bf7 {[%eval -1040,29] [%emt 0:00:06]} Qh7 {[%eval 1643,32] [%emt 0:00:06]}


    Regards Les, Livingston, Scotland

    • Marked as answer by Les2011 Monday, May 6, 2019 4:56 PM
    Monday, May 6, 2019 12:55 PM
  • Hi Les,

    I think you got it <S>!!  Would you mind explaining to me how your regex expression works so I can understand it?  Thank you so much for your solution.

    Les

    Monday, May 6, 2019 3:50 PM
  • Hi Les,

    I think you got it <S>!!  Would you mind explaining to me how your regex expression works so I can understand it?  Thank you so much for your solution.

    Les

    Hi

    My understanding of Regular Expressions is very limited, and wouldn't extend to good explanatioms - mich more of a trial and error type thing with me.

    There are many good web sites that explain things accurately, best to use one of those.


    Regards Les, Livingston, Scotland

    Monday, May 6, 2019 4:27 PM
  • Hi Les,

    Well I appreciate your honesty and your help.

    I will look to see what I can find and your solution appears to be working fine.

    Thanks,

    Les

    Monday, May 6, 2019 4:56 PM
  • Hi

    Perhaps mark Viorel as answer rather than my post as I used his as the starting point.


    Regards Les, Livingston, Scotland

    Monday, May 6, 2019 5:28 PM
  • Good idea he too was helpful, thx Les
    Monday, May 6, 2019 7:54 PM
  • Hi Viorel,

    When I tried your code snippet it didn't give me the desired result however Les tweaked it a little and got it resolved.  Thank you for your input.

    Les

    Monday, May 6, 2019 7:56 PM