Regex help!

  1. 2 weeks ago

    Completely new to Regex(and Xojo for that matter) but am having to use it for this application at work as I was told it would be the easiest way.
    I know my search pattern works because I tested it in RegExRx. However, I cannot figure out how to get the info I need to be displayed in the textField.

    Note... I am trying to just get it in the Text Field for now, and will try to figure out how to get it added into a list box under the appropriate columns. But that's for another day.

    First - Everything works up to line 50. It displays the following info to the Text Box:
    kMDItemColorSpace = "RGB"
    kMDItemDisplayName = "getme.jpg"
    kMDItemKind = "JPEG image"
    kMDItemPhysicalSize = 16384
    kMDItemPixelHeight = 208
    kMDItemPixelWidth = 250
    kMDItemProfileName = (null)
    kMDItemResolutionHeightDPI = 96

    After line 50, when I get into the Regex Search stuff, is where I'm getting lost.
    What I want is to print the searched pattern to the text box. The pattern is simply taking the characters in-between the quotes and also any numbers, as I only care about that data. So it should output:
    RGB
    getme.jpg
    JPEG image
    16384
    208
    250
    (null)
    96

    I added in the MsgBox for quick output. Each time, it pops up with nothing.

    Here is my full code:

    dim sh as new Shell
    dim openFile as FolderItem
    
    dim fileName as String
    dim modFileName as String
    dim fileType as String
    dim fileSize as String
    dim imageColor as String
    dim profile as String
    dim dpi as String
    dim dimensions as String
    dim height as String
    dim width as String
    dim output() as String
    
    dim imageCommands(9) as String
    
    if obj.FolderItemAvailable then
      openFile = obj.FolderItem
      
      //shell commands
      fileName = "-name kMDItemDisplayName"
      fileType = "-name kMDItemKind"
      fileSize = "-name kMDItemPhysicalSize"
      imageColor = "-name kMDItemColorSpace"
      profile = "-name kMDItemProfileName"
      dpi = "-name kMDItemResolutionHeightDPI"
      height =  "-name kMDItemPixelHeight "
      width = "-name kMDItemPixelWidth "
      
      //Array of metadata attributes
      imageCommands(0) = fileName
      imageCommands(1) = fileType
      imageCommands(2) = fileSize
      imageCommands(3) = imageColor
      imageCommands(4) = profile
      imageCommands(5) = dpi
      imageCommands(6) = dimensions
      imageCommands(7) = height
      imageCommands(8) = width
      
      
      //Shell execution
      sh.Execute("mdls " + Join(imageCommands, " ") + openFile.ShellPath)
      
      dim s as string 
      s = sh.result
      
    //THIS IS LINE 50 =>  //txtResults.text = s 
      
      
      //Regex stuff
      Dim rg as New RegEx
      Dim myMatch as RegExMatch
      Dim result as String
      
      
      rg.SearchPattern="(?mi-Us)""([^""]*)""|(\d*)" //find everything inside quotes and any numbered strings 
      myMatch = rg.Search(s) //searches the returned shell string
      result = myMatch.SubExpressionString(1) //No matter what number i put in here, 0-2, nothing works
      
      if myMatch is nil then
        MsgBox("text not found")
        //txtResults.text="Text not found!"
      else
        MsgBox(result)
        //txtResults.text = result
        
      end if
    end if

    The problem is the pattern is matching between every character because the pattern after the alternator is \d*, which means "zero or more". Well, the space between two character counts as zero. :) Change the * to a + and you should get better results.

    "(?mi-Us)""([^""]*)""|(\d+)"
  2. Jean-Yves P

    Feb 11 Pre-Release Testers, Xojo Pro Europe (France, Besançon)
    Edited 2 weeks ago

    you must make a loop display the match until the match is nil you are at the end of the result list.
    take a look at the example in the manual : https://docs.xojo.com/RegEx.Search

  3. @Jean-YvesPochez you must make a loop display the match until the match is nil you are at the end of the result list.

    I had that code implemented as well and nothing happened . : -\

  4. Kem T

    Feb 11 Pre-Release Testers, Xojo Pro, XDC Speakers Answer New York

    The problem is the pattern is matching between every character because the pattern after the alternator is \d*, which means "zero or more". Well, the space between two character counts as zero. :) Change the * to a + and you should get better results.

    "(?mi-Us)""([^""]*)""|(\d+)"
  5. Kem T

    Feb 11 Pre-Release Testers, Xojo Pro, XDC Speakers New York

    (Note: I did not check the rest of the code. I defer to Jean-Yves in his additional suggestion.)

  6. @Kem T (Note: I did not check the rest of the code. I defer to Jean-Yves in his additional suggestion.)

    Thanks Kem- but changing it to a + does the same thing...In the case of the output string with multiple numbers, I want all of them anyway.

  7. Kem T

    Feb 11 Pre-Release Testers, Xojo Pro, XDC Speakers New York

    Can you repost your updated code?

    Also, the way you've written the pattern, text between quotes will be in SubExpressionString( 1 ), whereas a number will be In SubExpressionString( 2 ). You have to check the SubExpressionCount to know which you should be accessing.

    Or rewrite your pattern like this so the result will always be in SubExpressionString( 1 ):

    "(?mi-Us)(?|""([^""]*)""|(\d+))"
  8. Paul L

    Feb 11 Xojo Inc http://docs.xojo.com

    Using Kem's suggested expression with this code, I get the results shown below:

    //Regex stuff
    Dim rg As New RegEx
    Dim myMatch As RegExMatch
    Dim result As String
    
    Dim s As String = TextArea1.Text
    
    rg.SearchPattern = "(?mi-Us)""([^""]*)""|(\d+)" // find everything inside quotes and any numbered strings 
    
    myMatch = rg.Search(s) //searches the returned shell string
    
    Do
      If myMatch <> Nil Then
        TextArea2.AppendText(myMatch.SubExpressionString(0) + EndOfLine)
    
        myMatch = rg.Search
      End If
    Loop Until myMatch Is Nil

    -image-

  9. @Kem T Can you repost your updated code?

    Also, the way you've written the pattern, text between quotes will be in SubExpressionString( 1 ), whereas a number will be In SubExpressionString( 2 ). You have to check the SubExpressionCount to know which you should be accessing.

    Or rewrite your pattern like this so the result will always be in SubExpressionString( 1 ):

    "(?mi-Us)(?|""([^""]*)""|(\d+))"

    I guess I need to read up more on the subExpressionStrings. Not really sure what that means.
    In any case, I did change the pattern to match what you said and it worked! But my final question is why does it work with SubExpressionString( 1 ) AND SubExpressionString( 0 )?

  10. Kem T

    Feb 11 Pre-Release Testers, Xojo Pro, XDC Speakers New York

    In the regular expression world, SubExpressionString equals "subgroup" and captures what's between the parenthesis in the order the parenthesis appear. If the matching pattern were (a)(b)(c), SubExpressionString( 1 ) would be "a", SubExpressionString( 2 ) "b", and SubExpressionString( 3 ) "c". SubExpressionString( 0 ) is the entire match, in this example "abc".

  11. @Kem T In the regular expression world, SubExpressionString equals "subgroup" and captures what's between the parenthesis in the order the parenthesis appear. If the matching pattern were (a)(b)(c), SubExpressionString( 1 ) would be "a", SubExpressionString( 2 ) "b", and SubExpressionString( 3 ) "c". SubExpressionString( 0 ) is the entire match, in this example "abc".

    Awesome- that makes much more sense.
    One last question @Kem T- is there a way to get the data back in a certain order?

  12. Kem T

    Feb 11 Pre-Release Testers, Xojo Pro, XDC Speakers New York

    I'm not sure what you mean. Example?

  13. last week

    @Kem T I'm not sure what you mean. Example?

    When it outputs
    RGB
    getme.jpg
    JPEG image
    16384
    208
    250
    (null)
    96
    What determines the order that ^^ is returned in?

  14. Kem T

    Feb 11 Pre-Release Testers, Xojo Pro, XDC Speakers New York

    The original text determines the order.

or Sign Up to reply!