How to remove a sequence of any numbers from the end of a string

Hello,

How do you remove a sequence of arbitrary numbers from the end of a string in sclang?
There is no replaceRegexp or similar, so my temporary solution is as follows, but I am not sure if this is the right way:

(
~replaceRegexp = { |string, findRegexp, replace|
	var found = string.findRegexp(findRegexp);
	string.replace(found[0][1], replace) 
}
)

~replaceRegexp.("123qwe456", "\\d+$", "")

You could use:

PathName("floating1").noEndNumbers.postln;

Best,
Paul

1 Like

Thanks for your suggestion. I think it is perfect for handling filenames. After looking at noEndNumbers and my code, I opened another thread and @jordan made a PR:

If it is merged, we can change strings more flexibly!

Actually it will work with any string, not just filenames, and it returns a string:

(
var myString = "(, : £$) This  is not a file name 12345";
var newString = PathName(myString).noEndNumbers;
newString.postcs;
newString.class.postln;
)

Of course, using RegExp is much more flexible for all kinds of tasks, but also requires knowledge and effort. So it may be overkill if you just want to remove the final numbers of a string.
Best,
Paul

1 Like

@prko Yes, I agree with @TXMod , it’s totally overkill!!!

But if you know this stuff, people will want you to help with very specific niches. I remember people using it a lot on the Arch Linux team, and years ago in Genetic Engineering.

EDIT: @prko There are MUCH SIMPLER parser techniques that you could use. It’s much more clear to understand and learn, and you can glue parsers together to build all sorts of things.

1 Like

For quality information about the topic:

1 Like

@TXMod @smoge
Yes, it is overkill. (I just realised that for the first time!)

Whenever I use the regular expression, I spend too much time trying to understand it and find the right solution. I thought this was due to my lack of computer science knowledge.

I did not know about Pathname.noEndNumbers until @TXMod mentioned it. However, in other cases I insisted on using regular expressions when I could use several `.replace’ methods to reduce lines of code.
I did not think this was overkill, although it took me a very long time to do it, and I thought this was one of those things I should understand.

Thank you for pointing out my unproductive attitude to programming. I should concentrate more on my goal!

1 Like

You’re smart and have enthusiasm, you totally can graps regexp if you want. Its not a “bad” thing". We said that in that particular case ONLY. Just evaluate what you need and want to know to do your stuff.

I think you just concentrated in regexp because sclang does not have good parser libraries, that’s all, I think, I remember your ad hoc DSL, that was wild.

1 Like

For instance, I’m slowly coding an interpreter for Scheme, written in Haskell. (For some reason, that’s a popular thing to do… haha) The experience of working with the parser was remarkably smooth, far surpassing what one might expect. Things like error handling can be very complex if you want them to be, though. It depends on what you want.

But with parsing, using a well-designed library, constructing the parser felt akin to assembling Legos—starting from small parsers, building up to medium ones, and eventually piecing together a comprehensive parser. Unlike the often cryptic nature of regular expressions, there were no hieroglyphic-like puzzles to decipher. BUT regular expressions can be very powerful in the right application!!!

hashVal :: Parser LispVal
hashVal = lexeme $ char '#'
  *> (char 't' $> Bool True
  <|> char 'f' $> Bool False
  <|> char 'b' *> (Number <$> intRadix (2, oneOf "01"))
  <|> char 'o' *> (Number <$> intRadix (8, octDigit))
  <|> char 'd' *> (Number <$> intRadix (10, digit))
  <|> char 'x' *> (Number <$> intRadix (16, hexDigit))
  <|> oneOf "ei" *> fail "Unsupported: exactness"
  <|> char '(' *> fail "Unsupported: vector"
  <|> char '\\' *> fail "Unsupported: char")


lispVal :: Parser LispVal
lispVal = hashVal
  <|> Nil <$ nil
  <|> Number <$> try (sign <*> decimal)
  <|> Atom <$> identifier
  <|> String <$> textLiteral
  <|> _Quote <$> quoted lispVal
  <|> List <$> parens manyLispVal
1 Like