Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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:

    /(?: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?
[Corion]: Hi marto!
[Corion]: I hope all is well marto! ;) My godson had a surprise visit to the hospital yesterday because he fell and had cut his skin besides his eye, but everything was glued together again and all is well
[marto]: good grief, that's not fun, glad to hear all is as well as could be :)
[Corion]: marto: Yeah - their mother picked all three of them up at the kindergarden to then go to the hospital, and all three of them were well behaved, and all also were quite obedient when they came home, so they recognized the situation

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2016-12-08 09:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (137 votes). Check out past polls.