Author Topic: Challenge: String parsers Revisited  (Read 2073 times)

0 Members and 1 Guest are viewing this topic.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10365
Challenge: String parsers Revisited
« on: October 16, 2006, 09:55:29 AM »
We've addressed this subject before although I can't find th thread.
So here it is again and with a twist.

First a string parser to separate the string into a list of strings using a
delimiter character.
This string "one,two,three,four" becomes '("one" "two" "three" "four")


Then a parser that allows for the use of an over ride of the delimiter.
So this "This|is|a`|test|string" becomes this '("This" " is" "a'|test" "string")

Although my routine did not remove override character perhaps it should.
So the result would be this  '("This" " is" "a|test" "string")

Here are some form that I tested on mine.
Code: [Select]
(defun c:test ()                                         
  (print (sparser "This|is|a`|test|string" "|" "`"))   
  (print (sparser "|This|is|a`|test|string|" "|" "`"))
  (print (sparser "`|This|is|a`|test|string`|" "|" "`"))
  (print (sparser "" "|" "`"))                         
  (print (sparser "`|This|is|a`|test|string`|" "|" ""))
  (princ)                                               
)

I clean up the list returned like this:
Code: [Select]
(vl-remove "" (sparser "This|is|a`|test|string" "|" "`")) removing any empty text.
and or use this to remove the override characters
Code: [Select]
(mapcar '(lambda(x) (vl-string-subst "" skp x)) lst)<edit: spelling correction>
« Last Edit: October 16, 2006, 10:57:53 AM by CAB »
I've reached the age where the happy hour is a nap. ()
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MP

  • Seagull
  • Posts: 17446
Re: Challenge: String Phrasers Revisited
« Reply #1 on: October 16, 2006, 10:35:30 AM »
Do you mean string parser Alan?

:)
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

CAB

  • Global Moderator
  • Seagull
  • Posts: 10365
Re: Challenge: String parsers Revisited
« Reply #2 on: October 16, 2006, 10:51:06 AM »
Yes!  :oops:

I guess there is no hope for me and my spelling.
Thanks for the help.
I've reached the age where the happy hour is a nap. ()
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MP

  • Seagull
  • Posts: 17446
Re: Challenge: String parsers Revisited
« Reply #3 on: October 16, 2006, 10:52:30 AM »
I guess there is no hope for me and my spelling. Thanks for the help.

No worries man -- someone had to take over for John.

:lol:
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1540
  • Moscow (Russia)
Re: Challenge: String parsers Revisited
« Reply #4 on: October 16, 2006, 11:46:52 AM »
Code: [Select]
(defun str-str-lst (str pat / i)
  (cond ((= str "") nil)
        ((setq i (vl-string-search pat str))
         (cons (substr str 1 i)
               (str-str-lst (substr str (+ (strlen pat) 1 i)) pat)
         ) ;_  cons
        )
        (t (list str))
  ) ;_  cond
) ;_  defun
Code: [Select]
(str-str-lst "This|is|a`|test|string" "|" )
;=>'("This" "is" "a`" "test" "string")
(str-str-lst "|This|is|a`|test|string|" "|" )
;=>'("" "This" "is" "a`" "test" "string")
(str-str-lst "`|This|is|a`|test|string`|" "|" )
;=>'("`" "This" "is" "a`" "test" "string`")
(str-str-lst "" "|" )
;=> nil
(str-str-lst "`|This|is|a`|test|string`|" "|" )
;=>'("`" "This" "is" "a`" "test" "string`")
Code: [Select]
(str-str-lst "This|is|a`|test|string" "`|" )
;=> '("This|is|a" "test|string")
(str-str-lst "|This|is|a`|test|string|" "`|" )
;=>'("|This|is|a" "test|string|")
(str-str-lst "`|This|is|a`|test|string`|" "`|" )
;=>'("" "This|is|a" "test|string")
(str-str-lst "" "`|" )
;=> 'nil
(str-str-lst "`|This|is|a`|test|string`|" "`|" )
;=>'("" "This|is|a" "test|string")
Code: [Select]
(str-str-lst "This|is|a`|test|string" "`" )
;=>'("This|is|a" "|test|string")
(str-str-lst "|This|is|a`|test|string|" "`" )
;=>'("|This|is|a" "|test|string|")
(str-str-lst "`|This|is|a`|test|string`|" "`" )
;=>'("" "|This|is|a" "|test|string" "|")
(str-str-lst "" "`" )
;=>'nil
(str-str-lst "`|This|is|a`|test|string`|" "`" )
;=>'("" "|This|is|a" "|test|string" "|")
蝸牛そろそろ登れ富士の山 /Kobayashi Issa/

CAB

  • Global Moderator
  • Seagull
  • Posts: 10365
Re: Challenge: String parsers Revisited
« Reply #5 on: October 16, 2006, 11:55:20 AM »
Thanks Evgeniy
Now one to do this:
Quote
Then a parser that allows for the use of an over ride of the delimiter.
So this "This|is|a`|test|string" becomes this '("This" " is" "a'|test" "string")
This will skip the `| just like wcmatch will ignore this `*
I've reached the age where the happy hour is a nap. ()
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1540
  • Moscow (Russia)
Re: Challenge: String parsers Revisited
« Reply #6 on: October 16, 2006, 12:03:47 PM »
Thanks Evgeniy
Now one to do this:
Quote
Then a parser that allows for the use of an over ride of the delimiter.
So this "This|is|a`|test|string" becomes this '("This" " is" "a'|test" "string")
This will skip the `| just like wcmatch will ignore this `*


It is my old program, from my lessons on recursion...
蝸牛そろそろ登れ富士の山 /Kobayashi Issa/

T.Willey

  • Needs a day job
  • Posts: 5218
Re: Challenge: String parsers Revisited
« Reply #7 on: October 16, 2006, 12:23:56 PM »
How about this one.
Code: [Select]
(defun StrParsev02 (String Seperator / Pos1 Pos2 NewStrList)

(setq Pos2 1)
(while (setq Pos1 (vl-string-search Seperator String Pos1))
 (if
  (and
   (> Pos1 1)
   (/= (substr String Pos1 1) "`")
  )
  (progn
   (if (= Pos2 1)
    (setq NewStrList (cons (substr String Pos2 Pos1) NewStrList))
    (setq NewStrList (cons (substr String Pos2 (- (1+ Pos1) Pos2)) NewStrList))
   )
   (setq Pos2 (1+ (+ (strlen Seperator) Pos1)))
  )
 )
 (setq Pos1 (+ Pos1 (strlen Seperator)))
)
(reverse (setq NewStrList (cons (substr String Pos2) NewStrList)))
)
Code: [Select]
(defun c:test ()                                         
  (print (StrParsev02 "This|is|a`|test|string" "|"))   
  (print (StrParsev02 "|This|is|a`|test|string|" "|"))
  (print (StrParsev02 "`|This|is|a`|test|string`|" "|"))
  (print (StrParsev02 "" "|"))                         
  (print (StrParsev02 "`|This|is|a`|test|string`|" "|"))
  (princ)                                               
)
Quote
Command: test

("This" "is" "a`|test" "string")
("|This" "is" "a`|test" "string" "")
("`|This" "is" "a`|test" "string`|")
("")
("`|This" "is" "a`|test" "string`|")
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10365
Re: Challenge: String parsers Revisited
« Reply #8 on: October 16, 2006, 07:19:25 PM »
Thanks Tim.

Here are my routines

Code: [Select]
  ;;++++++++++++++++++
  ;;  parser by CAB 
  ;;++++++++++++++++++
  ;;  this one removes parts of the source string
  (defun sparaser (str delim / ptr lst)
    (while (setq ptr (vl-string-search delim str))
      (setq lst (cons (substr str 1 ptr) lst))
      (setq str (substr str (+ ptr 2)))
    )
    (reverse (cons str lst))
  )


Code: [Select]
  ;;  This one uses pointers
  (defun sparser (str delim / ptr lst stp)
    (setq stp 1)
    (while (setq ptr (vl-string-search delim str (1- stp)))
        (setq lst (cons (substr str stp (- (1+ ptr) stp)) lst))
        (setq stp (+ ptr 2))
    )
    (reverse (cons (substr str stp) lst))
  )





 
Code: [Select]
;; This one uses pointers & allows a skip feature
 
  ;;++++++++++++++++++
  ;;  parser by CAB 
  ;;++++++++++++++++++
  ;;  (sphraser "This|is|a`|test|string" "|" "`")
  ;;  ("This" "is" "a`|test" "string")
  (defun sparser (str delim skp / ptr lst stp st1)
    (setq stp 1
          st1 1)
    (while (setq ptr (vl-string-search delim str (1- stp)))
      (if (/= (substr str (max ptr 1) 1) skp)
        (setq lst (cons (substr str st1 (- (1+ ptr) st1)) lst)
              st1 (+ ptr 2))
        (setq st1 stp)
      )
      (setq stp (+ ptr 2))
    )
    (reverse (cons (substr str st1) lst))
  )
I've reached the age where the happy hour is a nap. ()
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.