RegExp Global Search

Post

Posted
Rating:
#1 (In Topic #1016)
Regular
JumpyVB is in the usergroup ‘Regular’
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?

Code (gambas)

  1. ' Use "gb.pcre"
  2. Public Sub Form_Show()
  3.   Dim mySubject As String = "ExifTool Version Number         : 12.58\nFile Size                       : 2.3 MB\nMegapixels                      : 12.6\nShutter Speed                   : 1/50\n"
  4.   Dim myPattern As String = "^((File Size)|(Camera Model Name)|(Image Size)|(Megapixels)|(Shutter Speed)).*"
  5.   Dim myRegex As New RegExp(mySubject, myPattern, RegExp.MultiLine + RegExp.UTF8)
  6.   TextArea1.Text = myRegex.Text
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Guru
cogier is in the usergroup ‘Guru’
Is this what you are after?

Code (gambas)

  1. Public Sub Form_Show()
  2.  
  3.   Dim mySubject As String = "ExifTool Version Number         : 12.58\nFile Size                       : 2.3 MB\nMegapixels                      : 12.6\nShutter Speed                   : 1/50\n"
  4.  
  5.   TextArea1.Font = Font["monospace,12"]
  6.   TextArea1.Text = mySubject
  7.  

<IMG src="https://www.cogier.com/gambas/Exif1.png"> </IMG>
Online now: No Back to the top

Post

Posted
Rating:
#3
Regular
JumpyVB is in the usergroup ‘Regular’

cogier said

Is 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 RegExr: Learn, Build, & Test RegEx 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.
Online now: No Back to the top

Post

Posted
Rating:
#4
Guru
BruceSteers is in the usergroup ‘Guru’
i have never used regexp but the wiki clearly shows the use.  maybe have a read of that and all the methods for RegExp

/comp/gb.pcre/regexp - Gambas Documentation

Code (gambas)

  1. Dim sDiskIO, sVal As String
  2. Dim rMatch As New RegExp
  3.  
  4. ' get disk I/O stats
  5. Exec ["vmstat", "-D"] To sDiskIO
  6. For Each sVal In ["total reads", "read sectors", "writes", "written sectors"]
  7.   rMatch.Compile("^\\s*(\\d+)\\s+" & sVal, RegExp.MultiLine)
  8.   rMatch.Exec(sDiskIO)
  9.   If rMatch.Count = 1 Then
  10.     cVal[Replace(sVal, " ", "_")] = rMatch[1].Text
  11.   Else
  12.     Error.Raise("Missing '" & sVal & "' in 'vmstat -D' output")
  13. Print "total reads: " & cVal!total_reads & " read sectors:" & cVal!read_sectors
  14. Print "writes: " & cVal!writes & " written sectors: " & cVal!written_sectors
  15.  
Online now: No Back to the top

Post

Posted
Rating:
#5
Guru
BruceSteers is in the usergroup ‘Guru’
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..

Code (gambas)

  1. Public Sub Main()
  2.  
  3.   Dim mySubject As String = "ExifTool Version Number         : 12.58\nFile Size                       : 2.3 MB\nMegapixels                      : 12.6\nShutter Speed                   : 1/50\n"
  4.   Dim myPattern As String = "^((File Size)|(Camera Model Name)|(Image Size)|(Megapixels)|(Shutter Speed)).*"
  5.   Dim myRegex As New RegExp(mySubject, myPattern, RegExp.MultiLine + RegExp.UTF8)
  6.  
  7.  
  8. ' list all the matches...
  9. For c As Integer = 0 To myRegEx.Count - 1
  10.  TextArea1.Text &=  myRegex[c].Text & "\n"
  11.  
  12.  

But it does not seem to work, maybe the pattern is not right?
Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Guru
cogier is in the usergroup ‘Guru’
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.

Code (gambas)

  1. ' Gambas class file
  2.  
  3. ''Requires gb.gui
  4. ''ExifTool needs to be installed
  5.  
  6. Splitter1 As Splitter
  7. Splitter2 As Splitter
  8. FileChooser1 As FileChooser
  9. PictureBox1 As PictureBox
  10. TextArea1 As TextArea
  11.  
  12. Public Sub Form_Show()
  13.  
  14.   BuildForm
  15.   FileChooser1.Dir = User.Home &/ "Pictures"
  16.   Splitter1.Layout = [50, 50]
  17.   Splitter2.Layout = [75, 25]
  18.  
  19.  
  20. Public Sub FileChooser1_Change()
  21.  
  22.   Dim sRequired As String[] = ["File Size", "Camera Model Name", "Image Size", "Megapixels", "Shutter Speed"]
  23.   Dim sPhoto, sResult As String
  24.   Dim sExif As String[]
  25.   Dim iExif, iReq As Integer
  26.  
  27.   PictureBox1.Picture = Picture[FileChooser1.SelectedPath]
  28.   Shell "exiftool " & FileChooser1.SelectedPath To sPhoto
  29.  
  30.   sExif = Split(sPhoto, gb.NewLine, "", True)
  31.  
  32.   For iExif = 0 To sExif.Max
  33.     For iReq = 0 To sRequired.Max
  34.       If InStr(sExif[iExif], sRequired[iReq]) > 0 Then sResult &= sExif[iExif] & gb.NewLine
  35.     Next
  36.   Next
  37.  
  38.   TextArea1.Text = sResult
  39.  
  40.  
  41. Public Sub BuildForm()
  42.  
  43.   With Me
  44.     .Height = 800
  45.     .Width = 1300
  46.     .Arrangement = Arrange.Vertical
  47.     .Padding = 5
  48.     .Center
  49.  
  50.   With Splitter1 = New Splitter(Me) As "Splitter1"
  51.     .Expand = True
  52.     .Spacing = True
  53.  
  54.   With FileChooser1 = New FileChooser(Splitter1) As "FileChooser1"
  55.     .Expand = True
  56.     .ShowPreview = True
  57.  
  58.   With Splitter2 = New Splitter(Splitter1) As "Splitter2"
  59.     .Arrangement = Arrange.Vertical
  60.     .Expand = True
  61.  
  62.   With PictureBox1 = New PictureBox(Splitter2) As "PictureBox1"
  63.     .Mode = PictureBox.Contain
  64.     .Alignment = Align.Center
  65.     .Expand = True
  66.  
  67.   With TextArea1 = New TextArea(Splitter2) As "TextArea1"
  68.     .Expand = True
  69.     .Font = Font["monospace,12"]
  70.  
  71.  

<IMG src="https://www.cogier.com/gambas/ExifTest.png"> </IMG>
Online now: No Back to the top

Post

Posted
Rating:
#7
Regular
JumpyVB is in the usergroup ‘Regular’

BruceSteers said

Maybe the pattern is not right?
The patter is working elsewhere. I am using regex from grep for now:

Code

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

Code

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?
Online now: No Back to the top

Post

Posted
Rating:
#8
Guru
BruceSteers is in the usergroup ‘Guru’
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

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.
Online now: No Back to the top
1 guest and 0 members have just viewed this.