Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: How to trim a line from leading and trailing blanks without using regex or non-standard modules

by LanX (Cardinal)
on Aug 14, 2020 at 03:28 UTC ( #11120705=note: print w/replies, xml ) Need Help??


in reply to How to trim a line from leading and trailing blanks without using regex or non-standard modules

> which clearly is an abuse of regex.

Why is it an abuse of regex?

Problem is that \s is a meta character for any white-space not only blank " " , but only usable inside regex.°

So if you want the exact same semantic, it'll become far more complicated than this regex.

But better define your own trim() using a regex inside.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

°) compare Re^3: How to trim a line from leading and trailing blanks without using regex or non-standard modules

  • Comment on Re: How to trim a line from leading and trailing blanks without using regex or non-standard modules
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: How to trim a line from leading and trailing blanks without using regex or non-standard modules
by likbez (Sexton) on Aug 14, 2020 at 19:39 UTC
    So if you want the exact same semantic, it'll become far more complicated than this regex.

    I agree. That's a good point. Thank you !

    In other words it is not easy to design a good trim function without regex, but it is possible to design one that used regex, but treating the single quoted string as a special case

    For example

    trim(' ',$line)
    vs
    trim(/\s/.$line)
    BTW this is impossible in Python which implements regex via library, unless you add a new lexical type to the Language (regex string instead of raw string that is used).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11120705]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2020-09-18 23:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I don’t succeed, I …










    Results (113 votes). Check out past polls.

    Notices?