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

Answer: How do I print only some letters of this string

by jlistf (Monk)
on Jun 21, 2000 at 18:04 UTC ( #19243=categorized answer: print w/replies, xml ) Need Help??

Q&A > input and output > How do I print only some letters of this string - Answer contributed by jlistf

i see two ways to answer this question. you could use a regex: (assuming the data is in $_):
m/(\d{4}/\d{2}/\d{2}\s*?\d{2}:\d{2}:\d{2}\s*?$name\s*Communications Al +arm\s*Critical|Minor\s*?.{17})/; $data = $1;
that expression may need some debugging, but thats the basic idea. $name can be replaced by something to represent the N1B1,etcs. i don't know what that part of the data is so i'm not sure how to represent it in a regex. second option:
($date, $time, $name, $comm, $alarm, $severity, $data) = split ' ',$_, +7; $data = substr ($data,0,17);
again this might need a little tweaking, but thats the basic idea. there are other ways as well, but these are the two that popped into my head.

Replies are listed 'Best First'.
RE: Answer: How do I print only some letters of this string
by nuance (Hermit) on Jun 21, 2000 at 19:39 UTC

    I don't think this regular expression does what you think it does. You have not bounded that | term, so it splits your entire regular expression into two. Also you don't need all the stuff at the start since you aren't actually doing anything with it (storing it etc.)

    You could achieve your goal with:


    The (?: term groups without creating a back reference. The \s should not use non-greedy matching, using non-greedy matching here will include leading space in the 17 characters if there is more than one space separating the final two fields. Also we want at least one character at that position so + is better than *. The final field needs to be limited to 17 characters, but if there are less, your expression will fail instead of returing as many as it can find.


      good points all of them. i probably should have done a little more debugging on my answer, but i figured the gist would come across. you could also use ?<= instead of ?: but thats a minor detail.
Log In?

What's my password?
Create A New User
and not a whimper to be heard...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2018-06-25 19:59 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (128 votes). Check out past polls.