"Find and replace" range of numbers

"Find and replace" range of numbers

cherrygate
Enthusiast Enthusiast
989 Views
11 Replies
Message 1 of 12

"Find and replace" range of numbers

cherrygate
Enthusiast
Enthusiast

I have this script I am currently using to remove crap data

 

(defun c:Fix_Labels
  (/ tss)(vl-load-com)
  (setq findText '("01." "02." "03." "04." "05." "06." "07." "08." "09." "10." "11." "12." "1900" "1901" "1902" "1903" "1904" "1905" "1906" "1907" "1908" "1909" "1910" "1911" "1912" "1913" "1914" "1915" "1916" "1917" "1918" "1919" "1920" "1921" "1922" "1923" "1924" "1925" "1926" "1927" "1928" "1929" "1930" "1931" "1932" "1933" "1934" "1935" "1936" "1937" "1938" "1939" "1940" "1941" "1942" "1943" "1944" "1945" "1946" "1947" "1948" "1949" "1950" "1951" "1952" "1953" "1954" "1955" "1956" "1957" "1958" "1959" "1960" "1961" "1962" "1963" "1964" "1965" "1966" "1967" "1968" "1969" "1970" "1971" "1972" "1973" "1974" "1975" "1976" "1977" "1978" "1979" "1980" "1981" "1982" "1983" "1984" "1985" "1986" "1987" "1988" "1989" "1990" "1991" "1992" "1993" "1994" "1995" "1996" "1997" "1998" "1999" "2000" "2001" "2002" "2003" "2004" "2005" "2006" "2007" "2008" "2009" "2010" "2011" "2012" "2013" "2014" "2015" "2016" "2017" "2018" "2019" "2020" "2021"))
  (setq replaceText "")
  (foreach item findText
    (if (setq ss (ssget "X" (list '(0 . "*TEXT") (cons 1 (strcat "*" item "*")))))
      (repeat (setq i (sslength ss))
        (setq ed (entget (ssname ss (setq i (1- i)))))
        (setq tx (cdr (assoc 1 ed)))
        (setq tx (vl-string-subst replaceText item tx))
        (entmod (subst (cons 1 tx) (assoc 1 ed) ed)))))


  (setq findText '("0000" "000"))
  (setq replaceText "P.")
  (foreach item findText
    (if (setq ss (ssget "X" (list '(0 . "*TEXT") (cons 1 (strcat "*" item "*")))))
      (repeat (setq i (sslength ss))
        (setq ed (entget (ssname ss (setq i (1- i)))))
        (setq tx (cdr (assoc 1 ed)))
        (setq tx (vl-string-subst replaceText item tx))
        (entmod (subst (cons 1 tx) (assoc 1 ed) ed)))))

  )

 

 

It needs to do it in the same order of find + replace that I have it doing now, but also there are two conditions it doesn't handle that I would like to:

 

1. "00" replace with P. - but ONLY after the script has evaluated the rest of my list, AND ONLY if it is at the beginning of a number (For example: 001234 would be P.1234 BUT 123400 would NOT be changed to be 1234P.)

 

2. After all of that is evaluated, I have a very large number range of numbers I want to be removed (3100000-3200000)

 

 

Any help to tweak this script, or create a whole new one would be highly appreciated. Thank you!

0 Likes
990 Views
11 Replies
Replies (11)
Message 2 of 12

pbejse
Mentor
Mentor

Please show some more example of the target and result.

"WHAT IT IS NOW" --> WHAT NOW" 

 

 

 

Message 3 of 12

marko_ribar
Advisor
Advisor

Maybe this could be useful :

http://www.theswamp.org/index.php?topic=4591.msg605179#msg605179 

Marko Ribar, d.i.a. (graduated engineer of architecture)
Message 4 of 12

ronjonp
Mentor
Mentor

@cherrygate wrote:

...

 

1. "00" replace with P. - but ONLY after the script has evaluated the rest of my list, AND ONLY if it is at the beginning of a number (For example: 001234 would be P.1234 BUT 123400 would NOT be changed to be 1234P.)

 

...


This should do that:

 

(if (setq s (ssget "_x" '((0 . "TEXT") (1 . "00*"))))
  (foreach e (mapcar 'cadr (ssnamex s))
    (entmod (append (entget e) (list (cons 1 (strcat "P." (substr (cdr (assoc 1 (entget e))) 3))))))
  )
)

 

Message 5 of 12

cherrygate
Enthusiast
Enthusiast

@pbejse wrote:

Please show some more example of the target and result.

"WHAT IT IS NOW" --> WHAT NOW" 

 


 There is so much crap data, it's hard to give a good example of that, unfortunately.

 

But those generic "rules" I said should handle most scenarios and the ones it doesn't probably have to be manually trimmed anyway.

0 Likes
Message 6 of 12

john.uhden
Mentor
Mentor

Seems to me you're gonna hafta use wcmatch to test if a string matches your intentions.

John F. Uhden

0 Likes
Message 7 of 12

pbejse
Mentor
Mentor

@cherrygate wrote:

"00" replace with P. - but ONLY after the script has evaluated the rest of my list, AND ONLY if it is at the beginning of a number (For example: 001234 would be P.1234 BUT 123400 would NOT be changed to be 1234P.)

 


On your post you are targetting 3 to four zeros , 

(setq findText '("0000" "000"))

does that now include two zeros as well?

 

 ...
(setq replaceText "P.")
   (if	(setq ss (ssget "_x" '((0 . "TEXT") (1 . "* 000*,000*")))
	     )
      (repeat (setq i (sslength ss))
	(setq ed (entget (ssname ss (setq i (1- i)))))
	(setq tx (cdr (assoc 1 ed)))
	(setq crap (vl-some '(lambda (z)
		    (if (vl-string-search z tx) z))
			  '("0000" "000")))
	(setq tx (vl-string-subst replaceText crap tx))
	(entmod (subst (cons 1 tx) (assoc 1 ed) ed))
      )
    )
...

HTH

 

0 Likes
Message 8 of 12

Sea-Haven
Mentor
Mentor

Like pbe a loop that looks at each character is it  "0" so you could have as many as you like "0000013"  then use substr to remake the value with a P.13 So my $0.05 Note the test values 0000 etc.

 

(setq findText '("00001." "002." "03." "000004." "0005." "006." "07." "08." "09." "10." "11." "12." "1900" "1901" "1902" "1903" "1904" "1905" "1906" "1907" "1908" "1909" "1910" "1911" "1912" "1913" "1914" "1915" "1916" "1917" "1918" "1919" "1920" "1921" "1922" "1923" "1924" "1925" "1926" "1927" "1928" "1929" "1930" "1931" "1932" "1933" "1934" "1935" "1936" "1937" "1938" "1939" "1940" "1941" "1942" "1943" "1944" "1945" "1946" "1947" "1948" "1949" "1950" "1951" "1952" "1953" "1954" "1955" "1956" "1957" "1958" "1959" "1960" "1961" "1962" "1963" "1964" "1965" "1966" "1967" "1968" "1969" "1970" "1971" "1972" "1973" "1974" "1975" "1976" "1977" "1978" "1979" "1980" "1981" "1982" "1983" "1984" "1985" "1986" "1987" "1988" "1989" "1990" "1991" "1992" "1993" "1994" "1995" "1996" "1997" "1998" "1999" "2000" "2001" "2002" "2003" "2004" "2005" "2006" "2007" "2008" "2009" "2010" "2011" "2012" "2013" "2014" "2015" "2016" "2017" "2018" "2019" "2020" "2021"))

(defun c:test ( / x y str f )
(repeat (setq y (length findtext))
	(setq str (nth (setq y (- y 1)) findtext))
	(if (= (substr str 1 1 ) "0")
	(progn
		(setq f "No" x 2)
		(while (= f "No")
		(setq ans (substr str x 1 ))
		(if (= ans "0")
			(setq x (+ x 1))
			(setq f "yes")
		)
		)
		(princ (strcat "\n" (rtos y 2 0)))
		(setq findtext (subst (strcat "P" (substr str x (- (strlen str) (- x 1)))) str findtext))
	)
	)
)
(princ)
)

(c:test)

 

 

 

 

0 Likes
Message 9 of 12

ronjonp
Mentor
Mentor

@Sea-Haven 

This is how I'd remove leading zeroes.

(defun c:foo (/ s)
  (if (setq s (ssget "_x" '((0 . "TEXT") (1 . "0*"))))
    (foreach e (mapcar 'cadr (ssnamex s))
      (entmod
	(append	(entget e)
		(list (cons 1 (strcat "P." (vl-string-left-trim "0" (cdr (assoc 1 (entget e)))))))
	)
      )
    )
  )
  (princ)
)

 

 

Message 10 of 12

stevor
Collaborator
Collaborator

And for #2, (3100000-3200000), try:

 (ssget "_x" '((0 . "TEXT") (1 . "31#####")))
 (ssget "_x" '((0 . "TEXT") (1 . "3200000")))

 

S
0 Likes
Message 11 of 12

ronjonp
Mentor
Mentor

@stevor wrote:

And for #2, (3100000-3200000), try:

 (ssget "_x" '((0 . "TEXT") (1 . "31#####")))
 (ssget "_x" '((0 . "TEXT") (1 . "3200000")))

 


Those could be combined like so 🙂

(ssget "_X" '((0 . "TEXT") (1 . "31#####,3200000")))

 

Message 12 of 12

pbejse
Mentor
Mentor

@stevor wrote:

And for #2, (3100000-3200000)try:


I take it the items on the list are month < "05." > and year < "1984" >

 

I still dont get what the OP meant by that range really, thats why i'm asking for more sample string.

If its just that range then its too easy to replace.  

 

0 Likes