Author Topic: ( Challenge ) Matching and returning strings  (Read 3036 times)

0 Members and 1 Guest are viewing this topic.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
( Challenge ) Matching and returning strings
« on: July 01, 2005, 08:30:15 AM »
Write a function that returns the lines of text that match '(<str>)' or '[<str>]' from a list of strings, as in read from a file.

Example:
Our text file contains the lines;
Code: [Select]

1 this is a line of text
2 this is a (line) of text
3 this is a line of text
4 this is a line of text
5 this [is] a line of text
6 this is a line of text



the function should return lines 2 and 5 only.

Note: Please give the newbies a chance at this before you post your answer. Thanks.
TheSwamp.org  (serving the CAD community since 2003)

whdjr

  • Guest
( Challenge ) Matching and returning strings
« Reply #1 on: July 01, 2005, 11:15:48 AM »
Mark,

Is this 'text' in a dwg or txt file?
Is it text or mtext or could be both?
If it is text are we to allow for multiple selections?

Am I taking this too far?
Are you just asking for the cut and dry function? :D

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
( Challenge ) Matching and returning strings
« Reply #2 on: July 01, 2005, 11:22:02 AM »
Quote from: whdjr
Mark,

Is this 'text' in a dwg or txt file?
Is it text or mtext or could be both?
If it is text are we to allow for multiple selections?

Am I taking this too far?
Are you just asking for the cut and dry function?

For the sake of this challenge let's say we're reading lines of text from a file.

Quote
Are you just asking for the cut and dry function?

I guess so ..... :roll:
TheSwamp.org  (serving the CAD community since 2003)

Amsterdammed

  • Guest
( Challenge ) Matching and returning strings
« Reply #3 on: July 02, 2005, 04:32:13 AM »
Mark,

like this?

Quote
(defun findstring (/ LINE LINES TXT)

  (setq txt (open (findfile "test.txt") "R"))

  (while (setq line (read-line txt))

    (if (or (and (vl-string-position (ascii "(") line)
                 (vl-string-position (ascii ")") line)
                 ) ;_ end of and
            (and (vl-string-position (ascii "[") line)
                 (vl-string-position (ascii "]") line)
                 ) ;_ end of and
            ) ;_ end of or
      (setq lines (append lines (list line)))
      ) ;_ end of if
    ) ;_ end of while
  (close txt)
  (mapcar '(lambda (x) (prompt (strcat "\n" x))) lines)
  (princ)

  )


:?:

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
( Challenge ) Matching and returning strings
« Reply #4 on: July 02, 2005, 08:11:58 AM »
Something like that. Try using 'wcmatch'.
TheSwamp.org  (serving the CAD community since 2003)

Amsterdammed

  • Guest
( Challenge ) Matching and returning strings
« Reply #5 on: July 02, 2005, 09:03:00 AM »
Hmm,

Mark,
why is

Quote
(wcmatch " this [is] a line of text " "*[*" )  


false and

Quote
(wcmatch " this [is] a line of text " "*]*" )

True?

is er somthing special with [ , like \ ??
I don't get this :roll:

Bernd

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
( Challenge ) Matching and returning strings
« Reply #6 on: July 02, 2005, 09:21:38 AM »
try using a " ` " (tick) to escape the " [ ] " and " ( )" in your pattern. remember you want to match "[<str>]" ; the whole thing.
TheSwamp.org  (serving the CAD community since 2003)

Amsterdammed

  • Guest
( Challenge ) Matching and returning strings
« Reply #7 on: July 02, 2005, 11:23:17 AM »
Mark.

Interesting. I
To be honest, I never used this before  (lack of knowledge). I only used the * as wildcard, but there is much more, I see.



Code: [Select]
(defun findstring (/ LINE LINES TXT)

  (setq txt (open (findfile "test.txt") "R"))

  (while (setq line (read-line txt))

    (if (or (wcmatch line "*`(*,*`)*")
            (wcmatch line "*`[*,*`]*")
            ) ;_ end of or
      (setq lines (append lines (list line)))
      ) ;_ end of if
    ) ;_ end of while
  (close txt)
  (mapcar '(lambda (x) (prompt (strcat "\n" x))) lines)
  (princ)

  )


Is that how you would do it? (I still always go the hard, long way; I'm afraid)
Thanks,
Bernd

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
( Challenge ) Matching and returning strings
« Reply #8 on: July 02, 2005, 06:20:57 PM »
That's what I was talking about. :)
TheSwamp.org  (serving the CAD community since 2003)

tcdan

  • Guest
( Challenge ) Matching and returning strings
« Reply #9 on: July 02, 2005, 08:01:05 PM »
Cool!  How do you think this could be used. . . I'm trying to think of some good applications.