Jump to content

StringRegExp Help


gcue
 Share

Recommended Posts

hello =)

i am trying to get all instances of the following:

the number of "-" changes

;<------- begin Word or Phrase ------- >

then I am trying to find all the matches for the same Word or Phrase that fall under this sequence

|Word or Phrase: blah blah.|

trying to get the blah blah

here is what i have so far...

$data = FileRead($file)

$names = StringRegExp($data, ";<-{x,5}begin", 1)

debug($names)

for $x = 1 to UBound($names)
$descriptions = StringRegExp($data, "|" & $names[$x] & ".|", 1)
Next


Func Debug($variable1 = "", $variable2 = "", $variable3 = "")

If IsArray($variable1) Then
_ArrayDisplay($variable1)
Else
If $variable2 <> "" Then
$variable1 &= @CRLF & $variable2
EndIf

If $variable3 <> "" Then
$variable1 &= @CRLF & $variable3
EndIf

ClipPut($variable1)
MsgBox(0, "Debug", $variable1)
EndIf

EndFunc ;==>Debug

thanks in advance!

Link to comment
Share on other sites

  • Moderators

gcue,

How about this:

#include <Array.au3>

$sString = ";<------- begin Word or Phrase ------- >" & @CRLF & _
           "|Word or Phrase: 1111111.|" & @CRLF & _
           ";<------- begin Word or Phrase ------- >" & @CRLF & _
           "|Word or Phrase: 2222222.|" & @CRLF & _
           ";<------- begin Word or Phrase ------- >" & @CRLF & _
           "|Word or Phrase: 3333333.|"

$iCount = UBound(StringRegExp($sString, "(;<-+)", 3))

$aRet = StringRegExp($sString, "(?U)Phrase:\s(.*)\.\|", 3)

_ArrayDisplay($aRet, $iCount)

Any questions? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

gcue,

Then post an example of the data that you are actually using and tell us which parts you want to extract. If you do not give us real data to work on then you are unlikely to get a usable response and we waste our time working on something that is worthless. :mad2:

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

sorry bout that..

here's a true data set sample (notice sometimes theres a _ instead of a space too - bleh

;<-------------------- begin MustHave --------------------->

;<-------------------- begin PC_Anywhere --------------------->

$changelog_all_entries[898] = 'New|PC Anywhere: option to PC Anywhere remote PCs where available.|0.4.1.7'

$changelog_all_entries[899] = 'Changed|MustHave: entries automatically sort alphabetically after editing or adding items.|0.4.1.8'

$changelog_all_entries[900] = 'Changes|MustHave: parent categories do not need to be selected for adding and editing items.|0.4.1.8'

thanks melba!
Link to comment
Share on other sites

  • Moderators

gcue,

So you want all the text that follows the colon on each of those lines? :huh:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

My first attempt

#include <Array.au3>

$sString = ";<------- begin MustHave ------- >" & @CRLF & _
"|MustHave: 1111111.|" & @CRLF & _
";<------- begin PC_Anywhere ------- >" & @CRLF & _
"|PC_Anywhere: 2222222.|" & @CRLF

$aPhrases = StringRegExp($sString, "(?i:;<-+\s*begin\s*)(\w+)", 3)

$sString = StringRegExpReplace( $sString, "(?mU)^(?![|]).*\R", '' ) ;delete the first and third line

Local $iUbound = UBound($aPhrases ), $aRet[$iUbound]
For $i = 0 To $iUbound-1
$aRet[$i] = StringRegExpReplace($sString, "(?isU:.*" & $aPhrases[$i] & ":\s*)(\d+)(?s:.*)", "\1")
ConsoleWrite("(?i:" & $aPhrases[$i] & ":\s*)(\d+)(?s:.*)" & @CR )
Next
_ArrayDisplay( $aPhrases )
_ArrayDisplay($aRet)

Assuming the Data to be a word and the value to be a digit

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

Another Attempt

#include <Array.au3>

$sString = ";<------- begin MustHave ------- >" & @CRLF & _
"|MustHave: 1111111.|" & @CRLF & _
";<------- begin PC_Anywhere ------- >" & @CRLF & _
"|PC_Anywhere: 2222222.|" & @CRLF

$sString = StringRegExpReplace($sString, "(?mU)^(?![|]).*\R", '') ;delete the first and third line

$aaRet = StringRegExp( $sString, "\|(\w+):\s*(\d+)", 4 )
$l = 0
For $i = 0 To UBound($aaRet) - 1
$l = $aaRet[$i]
ConsoleWrite( StringFormat( "Data:%-11s Value:%-11s\n", $l[1], $l[2] ) )
Next

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

  • Moderators

gcue,

Here you go:

#include <Array.au3>

$sString = ";<-------------------- begin MustHave --------------------->" & @CRLF & _
           "" & @CRLF & _
           ";<-------------------- begin PC_Anywhere --------------------->" & @CRLF & _
           "" & @CRLF & _
           "$changelog_all_entries[898] = 'New|PC_Anywhere: option to PC Anywhere remote PCs where available.|0.4.1.7'" & @CRLF & _
           "$changelog_all_entries[899] = 'Changed|MustHave: entries automatically sort alphabetically after editing or adding items.|0.4.1.8'" & @CRLF & _
           "$changelog_all_entries[900] = 'Changes|MustHave: parent categories do not need to be selected for adding and editing items.|0.4.1.8'" & @CRLF & _
           "$changelog_all_entries[898] = 'New|PC Anywhere: option to PC Anywhere remote PCs where available.|0.4.1.9'"

; Get list of begin "text"
$aBegin = StringRegExp($sString, "(?U)begin\s(.*)\s", 3)

_ArrayDisplay($aBegin)

; Now look for data
Local $aFinal[1] = [0]
For $i = 0 To UBound($aBegin) - 1
    ; Amend underscore and spaces to look for both
    $sLine = StringRegExpReplace($aBegin[$i], "[\s|_]", "\[\\s\|_\]")
    ; Extract lines
    $aRet = StringRegExp($sString, "(?U)" & $sLine & ":\s(.*)'", 3)
    ; Add to list
    _ArrayConcatenate($aFinal, $aRet)
    $aFinal[0] += UBound($aRet)
Next

_ArrayDisplay($aFinal)

I added in another line to show that it deals with underscores and spaces. ;)

SRE decode:

; Begin SRE
(?U)        - Not greedy
begin\s(.*)\s"  - capture anything between "begin " and " "

; Amend replace SRER
[\s|_]          - Look for either spaces or underscores

\[\\s\|_\]      - replace with a literal string representing the [\s|_] pattern

; Extract SRE
(?U)            - Not greedy
$sLine &    - the Begin text with optional space/underscore followed by 
:\s        - a colon and a space
(.*)        - capture everything up to the next
'         - single quote

All clear? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

gcue,

Glad I could help. :)

And the odd SRE problem certainly clears out the cobwebs! :D

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

hey melba,

actually some names were missed (ones with spaces and other punctuation i guess)

;<-------------------- begin Window Context Menu --------------------->

;<-------------------- begin Web, File, Folder Notes Links --------------------->

thanks again for all your help

Link to comment
Share on other sites

  • Moderators

gcue,

As I have already mentioned, you need to give us realistic data if you want useable SRE patterns. If you keep adding different cases after the event then it is small wonder that the SREs do not work. :mad2:

Now before I start on this, are there are any other little "gotcha"s hiding in your data? :huh:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

gcue,

This should work for all cases: ;)

#include <Array.au3>

$sString = ";<-------------------- begin MustHave --------------------->" & @CRLF & _
           "" & @CRLF & _
           ";<-------------------- begin PC_Anywhere --------------------->" & @CRLF & _
           "" & @CRLF & _
           ";<-------------------- begin Window Context Menu --------------------->" & @CRLF & _
           "" & @CRLF & _
           ";<-------------------- begin Web, File, Folder Notes Links --------------------->" & @CRLF & _
           "" & @CRLF & _
           "$changelog_all_entries[898] = 'New|PC_Anywhere: option to PC Anywhere remote PCs where available.|0.4.1.7'" & @CRLF & _
           "$changelog_all_entries[899] = 'Changed|MustHave: entries automatically sort alphabetically after editing or adding items.|0.4.1.8'" & @CRLF & _
           "$changelog_all_entries[900] = 'Changes|MustHave: parent categories do not need to be selected for adding and editing items.|0.4.1.8'" & @CRLF & _
           "$changelog_all_entries[898] = 'New|PC Anywhere: option to PC Anywhere remote PCs where available.|0.4.1.9'" & @CRLF & _
           "$changelog_all_entries[900] = 'Changes|Window Context Menu: another of gcues little gotchas|0.4.1.10'" & @CRLF & _
           "$changelog_all_entries[900] = 'Changes|Web, File, Folder Notes Links: and yet another one - when will it end?|0.4.1.11'"

; Get list of begin "text"
$aBegin = StringRegExp($sString, "(?U)begin\s(.*)\s-", 3) ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

_ArrayDisplay($aBegin)

; Now look for data
Local $aFinal[1] = [0]
For $i = 0 To UBound($aBegin) - 1
    ; Amend underscore and spaces to look for both
    $sLine = StringRegExpReplace($aBegin[$i], "[\s|_]", "\[\\s\|_\]")
    ; Extract lines
    $aRet = StringRegExp($sString, "(?U)" & $sLine & ":\s(.*)'", 3)
    ; Add to list
    _ArrayConcatenate($aFinal, $aRet)
    $aFinal[0] += UBound($aRet)
Next

_ArrayDisplay($aFinal)

All that as changed is the Begin SRE which now looks up to the " -" after the "name". :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

I have modified M23 script

#include <Array.au3>

$sString = ";<-------------------- begin MustHave --------------------->" & @CRLF & _
";<-------------------- begin Window Context Menu --------------------->" & @CRLF & _
";<-------------------- begin Web, File, Folder Notes Links --------------------->" & @CRLF & _
";<-------------------- begin PC_Anywhere --------------------->" & @CRLF & _
"" & @CRLF & _
"$changelog_all_entries[898] = 'New|PC_Anywhere: option to PC Anywhere remote PCs where available.|0.4.1.7'" & @CRLF & _
"$changelog_all_entries[899] = 'Changed|MustHave: entries automatically sort alphabetically after editing or adding items.|0.4.1.8'" & @CRLF & _
"$changelog_all_entries[900] = 'Changes|MustHave: parent categories do not need to be selected for adding and editing items.|0.4.1.8'" & @CRLF & _
"$changelog_all_entries[898] = 'New|PC Anywhere: option to PC Anywhere remote PCs where available.|0.4.1.9'"

; Get list of begin "text"
$aBegin = StringRegExp($sString, "begin ([ \w,]*) ", 3)

_ArrayDisplay($aBegin)


For $i = 0 To UBound($aBegin) - 1
; Amend underscore and spaces to look for both
$sLine = StringRegExpReplace($aBegin[$i], "[\s|_]", "\[\\s\|_\]")
; Extract lines
$aRet = StringRegExp($sString, "(?U)" & $sLine & ":\s(.*)'", 3)
; Show the Array
If _ArrayDisplay($aRet, $aBegin[$i]) Then ContinueLoop
; Not Found the match
MsgBox( 16, "Error", "Match Not Found" & @CR & $aBegin[$i] )
Next

Edit : I'm late :P

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

  • Moderators

gcue,

Despite my protests earlier, I will still be here if you run into any more problem lines. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...