Jump to content

Recommended Posts

Posted

Remove [####.##.##] from a string

 

eg.

"[2017.03.06][2016.12.10]Blueprint.dwg"

 

When removed

 

"Blueprint.dwg"

Posted (edited)
Remove [####.##.##] from a string

 

normally we would use wcmatch,

alternatively i use list method bcos i see the brackets [##]

 


(setq str "[2017.03.06][2016.12.10]blueprint.dwg")

(strcat	(vl-string-right-trim
  " "
  (apply 'strcat
	 (mapcar ''((x) (strcat (vl-princ-to-string x) " "))
		 (vl-remove-if 'vl-consp (read (strcat "(" (vl-string-translate "[]" "()" str) ")")))
		 )
	 )
  )
".dwg"
)


Edited by hanhphuc
accept string with space , vl-string-right-trim
Posted
normally we would use wcmatch,

alternatively i use list method bcos i see the brackets [##]

 

Disadvantage the string must be no spacing, example "blue_print.dwg"

"blue print.dwg" failed

 


(setq str "[2017.03.06][2016.12.10]blueprint.dwg")

(strcat
(vl-princ-to-string
 (car
   (vl-remove-if
     'vl-consp
     (read
(strcat "(" (vl-string-translate "[]" "()" str ) ")")
)
     )
   )
 ) ".dwg")

 

Hi hanhphuc,

Thank you so much ! :)

I 'd like to see use wcmatch method.:D

Posted

Using wcmatch :

; _$ (foo "[2017.03.06][2016.12.10]Blueprint.dwg") -> "Blueprint.dwg"
; _$ (foo "[2017.03.06][2016.12.10]This is a test.dwg") -> "This is a test.dwg"
(defun foo ( s )
 (cond 
   ( (wcmatch s  "`[####`.##`.##`]`[####`.##`.##`]*`.dwg")
     (substr s 25)
   )
 )
)

Posted
Using wcmatch :

; _$ (foo "[2017.03.06][2016.12.10]Blueprint.dwg") -> "Blueprint.dwg"
; _$ (foo "[2017.03.06][2016.12.10]This is a test.dwg") -> "This is a test.dwg"
(defun foo ( s )
 (cond 
   ( (wcmatch s  "`[####`.##`.##`]`[####`.##`.##`]*`.dwg")
     (substr s 25)
   )
 )
)

 

Sorry , [####.##.##] maybe is not Two, maybe is one or Three.

Posted

Another, using Regular Expressions:

(defun foo ( str / rgx )
   (if (setq rgx (vlax-get-or-create-object "vbscript.regexp"))
       (progn
           (vlax-put-property rgx 'global actrue)
           (vlax-put-property rgx 'pattern "\\[\\d{4}\\.\\d{2}\\.\\d{2}\\]")
           (setq str (vlax-invoke rgx 'replace str ""))
           (vlax-release-object rgx)
           str
       )
   )
)

_$ (foo "[2017.03.06]Blueprint.dwg")
"Blueprint.dwg"
_$ (foo "[2017.03.06][2016.12.10]Blueprint.dwg")
"Blueprint.dwg"
_$ (foo "[2017.03.06][2016.12.10][2016.12.10]Blueprint.dwg")
"Blueprint.dwg"

Posted
Sorry , [####.##.##] maybe is not Two, maybe is one or Three.

 

Ah, now I understood the task! Here:

; _$ (foo "[2013.12.10]Blueprint.dwg") -> "Blueprint.dwg"
; _$ (foo "[2015.08.10][2016.12.10]This is a test.dwg") -> "This is a test.dwg"
; _$ (foo "[2012.02.13][1991.08.10][2016.12.10]And Another test.dwg") -> "And Another test.dwg"
(defun foo ( s )
 (cond 
   ( (wcmatch s  "`[####`.##`.##`]*`.dwg") 
     (while (wcmatch s  "`[####`.##`.##`]*`.dwg")
       (setq s (substr s 13))
     )
   )
 )
)

 

Lee your code blews my mind!

Posted

Thanks Grrr :thumbsup:

 

Note that your code could become:

(defun foo ( s )
   (while (wcmatch s "`[####`.##`.##`]*`.dwg")
       (setq s (substr s 13))
   )
   s
)

This way, the function will return the original string rather than nil if the pattern is not found.

Posted

Note that your code could become:

 

Thanks Lee, sometimes I overthink the code and can't see easily that it could be shortened. :)

Posted

Lee , hanhphuc, Grrr, Thank you very much! 8)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...