Perl match regex for every line /g not working?

having another problem with perl and regex, sorry for being a badass perl noobie ;-)

I get the whole programm output via backticks in the variable $output.

My regex matches always just for the first appearing in the output but not for every appearance..

/g didn't work either..

$output = `$calling 2>>bla.txt`; if ($output =~ m/SegmentMean .+ Likelihood: ([-+]?\d+\.?\d+)/g) { print "$1\n"; }

Outputs from the Java Output is:

SegmentMean 0 Likelihood: -7.138117738068088 SegmentMean 1 Likelihood: -11.508156736772925 SegmentMean 2 Likelihood: -10.424706822552386 SegmentMean 3 Likelihood: -9.560308776447586 SegmentMean 4 Likelihood: -9.26826400149502 SegmentMean 5 Likelihood: -9.524685714290996 SegmentMean 6 Likelihood: -8.360030637461975 SegmentMean 7 Likelihood: -8.163275211919945 SegmentMean 8 Likelihood: -7.917569593466525 SegmentMean 9 Likelihood: -9.043243571260344

Up to SegmentMean 20 ..


Try going through the matches like an array (and use a better regex):

while ($output =~ m/SegmentMean \S+ Likelihood: ([-+]?\d+(\.\d+)?)/g) {
print "$1\n";

That should work.

You can even turn it into a single line:

print "$1\n" while ($output =~ /SegmentMean \S+ Likelihood: ([-+]?\d+(?:\.\d+)?)/g);

You're only printing the first match. $1 is the first match.

Category:regex Time:2012-01-06 Views:1
Tags: regex perl

Related post

  • Have regex ignore new lines and just match on a whole large string? 2010-08-25

    I have this string here: CREATE UNIQUE INDEX index555 ON SOME_TABLE ( SOME_PK ASC ); I want to match across the multiple lines and match the SQL statements (all of them, there will be many in 1 large string)... something like this, however I am only

  • Perl script to extract 2 lines before and after the pattern matching 2011-05-08

    my file is like line 1 line 2 line 3 target line 5 line 6 line 7 I can write a regex that matches the target. What all I need is I need to grab lines 2,3,5,6. Is there any way to do it? --------------Solutions------------- If you're not determined to

  • What is best way to remove duplicate lines matching regex from string using Python? 2008-10-03

    This is a pretty straight forward attempt. I haven't been using python for too long. Seems to work but I am sure I have much to learn. Someone let me know if I am way off here. Needs to find patterns, write the first line which matches, and then add

  • What are good Perl Pattern-matching / Regex Modules? 2010-07-15

    I've been having a need to do a lot of regex / pattern-matching stuff lately and, in looking at different examples / forum posts from my web searches it seems people sometimes mention that perl has good modules to help in simplifying pattern matching

  • Regex to match only the first line? 2010-09-11

    Is it possible to make a regex match only the first line of a text? So if I have the text: This is the first line. This is the second line. ... It would match "This is the first line.", whatever the first line is. --------------Solutions-------------

  • Perl - replace regex match with arbitrary operation on regex 2010-12-17

    I have the following line in my Perl script: s/\b(\w+)\b/ $replaces{$1} ? $replaces{$1} : $1 /g; I want to find all words in the string and if the word is in the array of known words replace it else keep it (ideally I want to do an arbitrary operatio

  • RegEx pattern to match over a single line or more 2011-02-22

    I'm parsing a log file to identify and retrieve information about failures. Regular Expressions seem to be the right way to go about this. Here's my initial pattern: \d{4}-\d{2}-\d{2} \d{2}.* This works for well for single lines like this: 2011-02-06

  • How to capture multiple regex matches, from a single line, into the $matches magic variable in Powershell? 2010-06-29

    Let's say I have the string "blah blah F12 blah blah F32 blah blah blah" and I want to match the F12 and F32, how would I go about capturing both to the Powershell magic variable $matches? If I run the following code in Powershell: $string = "blah bl

  • How to pipe Bash Shell command's ouput line by line to Perl for Regex processing? 2012-02-07

    I have some output data from some Bash Shell commands. The output is delimited line by line with "\n" or "". I would like to know that is there any way to pipe the output into Perl and process the data line by line within Perl (just like piping the o

  • Regex for incomplete lines within known start and end strings 2009-08-02

    I want to insert the following into a database: (#text1#,#text2#,#text3#,#text4#,#text5#,#text6#, #text7#, #text8#, #text9#), (#text1#,#text2#,#text3#,#text4#,#text5#,#text6#, #text7#, #text8#, #text9#), (#text1#,#text2#,#text3#,#text4#,#text5#,#text

  • having a regex replacing across lines, retain the newlines? 2011-01-26

    I'd like to have a substitute or print style command with a regex working across lines. And lines retained. $ echo -e 'a\nb\nc\nd\ne\nf\ng' | tr -d '\n' | grep -or 'b.*f' bcdef or $ echo -e 'a\nb\nc\nd\ne\nf\ng' | tr -d '\n' | sed -r 's|b(.*)f|y\1z|'

  • Perl match newline in `-0` mode 2012-02-05

    Question Suppose I have a file like this: I've got a loverly bunch of coconut trees. Newlines! Bahahaha Newlines! the end. I'd like to replace an occurence of "Newlines!" that is surrounded by blank lines with (say) NEWLINES!. So, ideal output is: I'

  • Make a Perl-style regex interpreter behave like a basic or extended regex interpreter 2008-10-22

    I am writing a tool to help students learn regular expressions. I will probably be writing it in Java. The idea is this: the student types in a regular expression and the tool shows which parts of a text will get matched by the regex. Simple enough.

  • regex with negative matching (ie, find string that _doesn't_ match regex) 2009-07-22

    I have a log file with the string "ERROR" on some lines. I want to delete every line that doesn't have ERROR so that I can see just what needs fixing. I was going to do something like the following in vim: %s/!(ERROR)// to replace non-error lines wit

  • Is there a better way of doing data validation in Perl than regex? 2009-07-30

    Is there a better way of doing data validation in Perl than regex? I have Perl code that does string and numeric data validation using complex regex, and this makes code difficult to read and follow, for everyone. --------------Solutions-------------

  • Problem with matching token in a line, using Scanner in Java 2009-08-03

    I need to match certain things from lines of an input text. The lines look like this: to be/ Σ _ Σ [1pos, 1neg] {0=1, 2=1} I am using the Scanner class to read each line of the text, and I have written the following code. However, something is not wo

  • Matching text on multiple lines which begins and ends with some characters 2010-03-09

    i have a source file something like String some_words_come_here{ //some string lines //some string lines //some string lines //some string lines }; I am using it in java Pattern.compile("(?m)^Strin.+\\};$", Pattern.MULTILINE | Pattern.DOTALL); but th

  • Match regex from right to left? 2010-06-07

    Is there any way of matching a regex from right to left? What Im looking for is a regex that gets MODULE WAS INSERTED EVENT LOST SIGNAL ON E1/T1 LINK OFF CRC ERROR EVENT CLK IS DIFF FROM MASTER CLK SRC OF from this input CLI MUX trap received: (022)

  • single split or match regex 2010-06-20

    9.jul. 3 dni od 205,00 EUR Is it possible with single regex (match opr split) get 205,00 number from string? It must be single regex and not 2 or 3 regex which give 205,00 as result. string looks like: <option value="9.7.2010|3">9.jul. 3 dni od

Copyright (C), All Rights Reserved.

processed in 2.811 (s). 13 q(s)