Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

( #19243=categorized answer: print w/ replies, xml ) Need Help??

Q&A > input and output > How do I print only some letters of this string 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.

Comment on Answer: How do I print only some letters of this string
Select or Download Code
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:

    /(?:Critical|Minor)\s+(.{0,17})/;

    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.

    Nuance

      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?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2014-09-21 08:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (167 votes), past polls