RegExp Global Search

New to Gambas? Post your questions here. No question is too silly or too simple.
Post Reply
JumpyVB
Posts: 75
Joined: Friday 11th September 2020 9:09am

RegExp Global Search

Post by JumpyVB »

My pattern should be able to match several lines from my subject. But I only get the first matching line. I cannot find compile option global from the RegExp class. Any ideas how to get the desired output?

' Use "gb.pcre"
Public Sub Form_Show()
  Dim mySubject As String = "ExifTool Version Number         : 12.58\nFile Size                       : 2.3 MB\nMegapixels                      : 12.6\nShutter Speed                   : 1/50\n"
  Dim myPattern As String = "^((File Size)|(Camera Model Name)|(Image Size)|(Megapixels)|(Shutter Speed)).*"
  Dim myRegex As New RegExp(mySubject, myPattern, RegExp.MultiLine + RegExp.UTF8)
  TextArea1.Text = myRegex.Text
End
User avatar
cogier
Site Admin
Posts: 1125
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: RegExp Global Search

Post by cogier »

Is this what you are after?

Public Sub Form_Show()

  Dim mySubject As String = "ExifTool Version Number         : 12.58\nFile Size                       : 2.3 MB\nMegapixels                      : 12.6\nShutter Speed                   : 1/50\n"

  TextArea1.Font = Font["monospace,12"]
  TextArea1.Text = mySubject

End


Image
JumpyVB
Posts: 75
Joined: Friday 11th September 2020 9:09am

Re: RegExp Global Search

Post by JumpyVB »

cogier wrote: Saturday 8th April 2023 9:08amIs this what you are after?
No. The original mySubject is actually 115 lines of output from exiftool. I made a simplified example so it would be easier for others to try the code. I want to extract the lines beginning with "File Size", "Camera Model Name", "Image Size", "Megapixels" and "Shutter Speed". Other lines should be discarded.

My regex pattern works fine with an online tool such as https://regexr.com but I am unable get same results in Gambas.

There are other ways to solve this like doing a like comparison line by line in a for loop. But I would like to use regex for this, for the sake of learning how regex is used in Gambas.
User avatar
BruceSteers
Posts: 1574
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: RegExp Global Search

Post by BruceSteers »

i have never used regexp but the wiki clearly shows the use. maybe have a read of that and all the methods for RegExp

https://gambaswiki.org/wiki/comp/gb.pcre/regexp

Dim sDiskIO, sVal As String
Dim cVal As New Collection
Dim rMatch As New RegExp

' get disk I/O stats
Exec ["vmstat", "-D"] To sDiskIO
For Each sVal In ["total reads", "read sectors", "writes", "written sectors"]
  rMatch.Compile("^\\s*(\\d+)\\s+" & sVal, RegExp.MultiLine)
  rMatch.Exec(sDiskIO)
  If rMatch.Count = 1 Then
    cVal[Replace(sVal, " ", "_")] = rMatch[1].Text
  Else
    Error.Raise("Missing '" & sVal & "' in 'vmstat -D' output")
  Endif
Next
Print "total reads: " & cVal!total_reads & " read sectors:" & cVal!read_sectors
Print "writes: " & cVal!writes & " written sectors: " & cVal!written_sectors
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1574
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: RegExp Global Search

Post by BruceSteers »

PS. for exiftool you should be able to extract the tags just using their names without spaces.

ie...

exiftool -FileSize /file/path
exiftool -ImageSize /file/path
exiftool -CameraModelName /file/path
exiftool -ShutterSpeed /file/path

and so on.

or...
exiftool -FileSize -ImageSize -CameraModelName -ShutterSpeed /file/path



But for RegExp i think you want somethijng like this..

Public Sub Main()

  Dim mySubject As String = "ExifTool Version Number         : 12.58\nFile Size                       : 2.3 MB\nMegapixels                      : 12.6\nShutter Speed                   : 1/50\n"
  Dim myPattern As String = "^((File Size)|(Camera Model Name)|(Image Size)|(Megapixels)|(Shutter Speed)).*"
  Dim myRegex As New RegExp(mySubject, myPattern, RegExp.MultiLine + RegExp.UTF8)


' list all the matches...
For c As Integer = 0 To myRegEx.Count - 1
 TextArea1.Text &=  myRegex[c].Text & "\n"
Next

End


But it does not seem to work, maybe the pattern is not right?
If at first you don't succeed , try doing something differently.
BruceS
User avatar
cogier
Site Admin
Posts: 1125
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: RegExp Global Search

Post by cogier »

I don't know much about RegExp, so I have tried to solve this another way. Run the following code in a new Graphical Application.

' Gambas class file

''Requires gb.gui
''ExifTool needs to be installed

Splitter1 As Splitter
Splitter2 As Splitter
FileChooser1 As FileChooser
PictureBox1 As PictureBox
TextArea1 As TextArea

Public Sub Form_Show()

  BuildForm
  FileChooser1.Dir = User.Home &/ "Pictures"
  Splitter1.Layout = [50, 50]
  Splitter2.Layout = [75, 25]

End

Public Sub FileChooser1_Change()

  Dim sRequired As String[] = ["File Size", "Camera Model Name", "Image Size", "Megapixels", "Shutter Speed"]
  Dim sPhoto, sResult As String
  Dim sExif As String[]
  Dim iExif, iReq As Integer

  PictureBox1.Picture = Picture[FileChooser1.SelectedPath]
  Shell "exiftool " & FileChooser1.SelectedPath To sPhoto

  sExif = Split(sPhoto, gb.NewLine, "", True)

  For iExif = 0 To sExif.Max
    For iReq = 0 To sRequired.Max
      If InStr(sExif[iExif], sRequired[iReq]) > 0 Then sResult &= sExif[iExif] & gb.NewLine
    Next
  Next

  TextArea1.Text = sResult

End

Public Sub BuildForm()

  With Me
    .Height = 800
    .Width = 1300
    .Arrangement = Arrange.Vertical
    .Padding = 5
    .Center
  End With

  With Splitter1 = New Splitter(Me) As "Splitter1"
    .Expand = True
    .Spacing = True
  End With

  With FileChooser1 = New FileChooser(Splitter1) As "FileChooser1"
    .Expand = True
    .ShowPreview = True
  End With

  With Splitter2 = New Splitter(Splitter1) As "Splitter2"
    .Arrangement = Arrange.Vertical
    .Expand = True
  End With

  With PictureBox1 = New PictureBox(Splitter2) As "PictureBox1"
    .Mode = PictureBox.Contain
    .Alignment = Align.Center
    .Expand = True
  End With

  With TextArea1 = New TextArea(Splitter2) As "TextArea1"
    .Expand = True
    .Font = Font["monospace,12"]
  End With

End


Image
JumpyVB
Posts: 75
Joined: Friday 11th September 2020 9:09am

Re: RegExp Global Search

Post by JumpyVB »

BruceSteers wrote: Saturday 8th April 2023 11:27pm Maybe the pattern is not right?
The patter is working elsewhere. I am using regex from grep for now:

Code: Select all

$ exiftool ~/Pictures/myphoto.jpg | grep -E '^((File Size)|(Camera Model Name)|(Image Size)|(Megapixels)|(Shutter Speed)).*'
The above command will output something like:

Code: Select all

File Size                       : 2.5 MB
Camera Model Name               : OnePlus Nord 2T 5G
Shutter Speed Value             : 1/50
Image Size                      : 3072x4096
Megapixels                      : 12.6
Shutter Speed                   : 1/50
Maybe gb.pcre is only able to do one single match - Could it really be so?
User avatar
BruceSteers
Posts: 1574
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: RegExp Global Search

Post by BruceSteers »

yes but easier to not use regexp at all and just use the exiftool -Tag name feature ;)

the following will give the same result...

Code: Select all

exiftool -FileSize -Make -ImageSize -Megapixels -ShutterSpeed ~/Pictures/myphoto.jpg
(couldn't get -CameraModelName to work but -Make gave the same string)

It does not look like any of us here have had much experience using gb.pcre so cannot be much help on how to properly use it i'm afraid.
If at first you don't succeed , try doing something differently.
BruceS
Post Reply