Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: how to split a string at [ ?

by Athanasius (Archbishop)
on Jul 05, 2018 at 07:04 UTC ( [id://1217927]=note: print w/replies, xml ) Need Help??


in reply to how to split a string at [ ?

Hello blaui,

You can just use a regex:

use strict; use warnings; for my $string ("BCInletTemperature = 90[C]", "BCInletTemperature = 33 +.56[C]") { print "$1\n" if $string =~ / = \s* ([0-9.]+) \[ /x; }

Output:

16:59 >perl 1906_SoPW.pl 90 33.56 17:03 >

The regex says: match an equals sign, followed by zero or more whitespace characters, followed by one or more digit-or-decimal-point characters, followed by a left square bracket; and capture the string of digit-or-decimal-point characters in $1. The /x modifier on the regex means to not match whitespace inside the regex: this just makes the regex easier to read.

Update: Note that it is necessary to escape the square bracket to tell the regex engine that this is a character to be matched literally, and not the beginning of a character class.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: how to split a string at [ ?
by AnomalousMonk (Archbishop) on Jul 05, 2018 at 13:25 UTC

    blaui:   Note that the regex  [0-9.]+ also matches substrings like '1.2.3.4' and '....'.


    Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-19 17:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found