Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Function for sorting with regular expressions


  • Please log in to reply
4 replies to this topic
cnoiz
  • Members
  • 7 posts
  • Last active: Dec 05 2009 10:00 PM
  • Joined: 23 Oct 2009
This works. I couldn't find another function that does this so I wrote one.
InputList: `n delimited list to be sorted
Expression: Regular Expression to sort by
SortOptions: Options to apply to sort (like R or C)
SubPats: number of subpatterns you want (leave blank to ignore)

Example:
TestList =
(
George12:45Yellow
Amy11:33Blue
Jimmy13:13Purple
Benji03:87Tan
)

msgbox % RegExSort(TestList, "(\d\d):(\d\d)", 2)
return

RegExSort(InputList, Expression, Subpats = 1, SortOptions = "N")
    {
        loop, parse, InputList, `n
            {
                RegExMatch(A_LoopField, Expression, Match)
                ;msgbox, s:%subpats%
				if SubPats!=1
					{
						loop, %SubPats%
							{
								indexmatch := Match%A_Index%
								Matched := Matched indexmatch
								;msgbox, Matched: %matched% A_Index: %A_Index% indexmatch: %indexmatch%
								indexmatch :=
							}
					}
				else
					{
						Matched := Match
					}
				If Matched !=
					{
						
						WorkingString := WorkingString A_LoopField "cnoiza`\" Matched "cnoizb`n"
					}
				matched :=
				;msgbox, a: %A_LoopField% `ne: %Expression%`ni: %InputList% `no: %SortOptions% `nm:%Match1% `nw:%WorkingString%
            }
		
        Sort, WorkingString, \ %SortOptions%
		;msgbox, w:%WorkingString% o:%SortOptions%
        Return RegExReplace(WorkingString, "cnoiza.*?cnoizb")
    }


MasterFocus
  • Moderators
  • 4323 posts
  • Last active: Jan 28 2016 01:38 AM
  • Joined: 08 Apr 2009
####################################################################################
This old forum will be archived ( see https://autohotkey.c...s-decommission/ ).
For my up-to-date code and information, check the following links:
http://git.io/master | https://github.com/MasterFocus | http://masterfocus.ahk4.net/ | https://sites.google...masterfocusahk/
####################################################################################


The following function is based on work by cnoiz (see previous post)
 
Function:
    RegExSort()

Description:
    Sorts an input list based on RegEx needle and matching order.

Additional info:
    < none >

Links:
    GitHub (see AutoHotkey - Functions - RegExSort)

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Antonio França -- git.io -- github.com -- ahk4.net -- sites.google.com -- ahkscript.org

Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.


nit-picker
  • Guests
  • Last active:
  • Joined: --

I suggest you use the following MsgBox as example instead:

MsgBox % RegExSort(TestList, "(.)\d+:(\d+)", 2)


I suggest you use the following Msgbox as an example for your function
MsgBox % "order:`nblank/default`n`n"     RegExSort(.+`t(\d+) : (\d+)",TestList) 
; should be
MsgBox % "order:`nblank/default`n`n"     RegExSort(".+`t(\d+) : (\d+)",TestList)
;--------------------------------------------------^

lol, your test cases are all missing a quote mark after RegExSort(" :wink:

Both of these functions will be useful. I'll study them both.

MasterFocus
  • Moderators
  • 4323 posts
  • Last active: Jan 28 2016 01:38 AM
  • Joined: 08 Apr 2009

your test cases are all missing a quote mark after RegExSort

Fixed. Probably occurred when I was inserting color=darkred. Thanks.

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Antonio França -- git.io -- github.com -- ahk4.net -- sites.google.com -- ahkscript.org

Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.


cnoiz
  • Members
  • 7 posts
  • Last active: Dec 05 2009 10:00 PM
  • Joined: 23 Oct 2009
Nice work! I like the option to change the order of sorting. This is much better than the method I was using (calling this function multiple times in a different order on the same string). :D