Not sure if this gets you as far as you want, but you could use a macro to 
associate names with paren-wrapped items:

#lang racket

(define-syntax re-match
  (syntax-rules ()
    [(_ str re name ...)
     (match str
       [(regexp re (list _ name ...))
        (list (list (quote name) name) ...)])]))

(define msg "2018-02-02T11:26:34 someuser some-computername01 233.194.20.110 
something broke")

(re-match msg
          #px"^([-\\dT:]+)\\s(\\w+)\\s([-\\w\\d]+)\\s([\\d\\.]+)\\s(.+)$"
          date username hostname ip message)

… produces:

'((date "2018-02-02T11:26:34")
  (username "someuser")
  (hostname "some-computername01")
  (ip "233.194.20.110")
  (message "something broke”))


This macro gets the names in much closer to the corresponding patterns than 
matching by index, but it doesn’t actually embed the names into the regexp.

John Clements


> On Feb 2, 2018, at 10:01 AM, nocher...@gmail.com wrote:
> 
> Sorry if I've missed this in the documentation, but I don't see it, and it is 
> starting to bother me.
> 
> In Powershell. Python, and Splunk I'm able to perform automatic field 
> extraction on strings and access the values of fields by name.  Is there a 
> way to do this in Racket?  Of course, pairing matches with field names by 
> index is an option, but not as convenient in some situations.
> 
> Take string "2018-02-02T11:26:34 someuser some-computername01 233.194.20.110 
> something broke" as a trivial example.
> 
> Powershell:
> "2018-02-02T11:26:34 someuser some-computername01 233.194.20.110 something 
> broke" -match 
> "^(?<Date>[\d\-T:]+)\s(?<Username>\w+)\s(?<Hostname>[\w\-\d]+)\s(?<IP>[\d\.]+)\s(?<Message>.+)$"
>  | Out-Null
> 
> $matches.date
> $matches.username
> $matches.hostname
> $matches.IP
> $matches.message
> 
> Python:
> m = 
> re.match("^(?P<Date>[\d\-T:]+)\s(?P<Username>\w+)\s(?P<Hostname>[\w\-\d]+)\s(?P<IP>[\d\.]+)\s(?P<Message>.+)$",
>  "2018-02-02T11:26:34 someuser some-computername01 233.194.20.110 something 
> broke")
> 
> m['Date']
> m['Username']
> m['Hostname']
> m['IP']
> m['Message']
> 
> Both output:
> 
> 2018-02-02T11:26:34
> someuser
> some-computername01
> 233.194.20.110
> something broke
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to