1

i have been able to read the data from a text file using Imagemagick from http://www.imagemagick.org.

and i get

0,0: (255,255,255,  0)  #FFFFFF00  srgba(255,255,255,0)
1,0: (255,255,255,  0)  #FFFFFF00  srgba(255,255,255,0)
2,0: (255,255,255,  0)  #FFFFFF00  srgba(255,255,255,0)


40,23: (162,167, 32, 24)  #A2A72018  srgba(162,167,32,0.0941176)
41,23: (255,255,255,  0)  #FFFFFF00  srgba(255,255,255,0)
42,23: (162,166, 48, 40)  #A2A63028  srgba(162,166,48,0.156863)
43,23: (162,166, 47, 40)  #A2A62F28  srgba(162,166,47,0.156863)

i am not good with regular expressions what kinda of expression would i use to get the coords at the beginning and the rgba() at the end.

ok i have kinda figured out the regular expression here is a par of it

/rgba\([0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0-9]\.[0-9]{1-9}/gi

but the last part is not matching to the decimal part of the rgba().

ok i know its been awhile and i am not sure if i should start a new thread

but i have figured out how to remove to remove the two middle parts, all the parentheses and srgba, also the lines with zero alpha, but somehow it leaves an open space in the text file. if there is any improvements that anybody can see.

$fh = fopen("pcmanD.txt", "r");
$fg = fopen("pcmanJ.txt", "wt");
$new_array = ""; $parts = "";
while (!feof($fh)) {
    $line = fgets($fh);
    $lines[] = $line;
    $newword = "";
    $match1 ="/:\s?\s?\(\s?\s?\d+,\s?\s?\d+,\s?\s?\d+,\s?\s?\d+\)\s?\s?#[a-zA-Z0-9]{6,8}\s?\s?srgba/";
    $match2 ="/^\s\s?/";
    $match3 = "/\s\(/";
    $match4 = "/\)/";
    $match5 = "/[0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0-9]{1,3},0[^\.]/";

    $parts1 = preg_replace($match1,"", $line );
    $parts2 = preg_replace($match2,"", $parts1 );
    $parts3 = preg_replace($match3,",", $parts2 );
    $parts4 = preg_replace($match4,"", $parts3 );
    $parts5 = preg_replace($match5,"",$parts4);


    echo "<pre>";
    print_r($parts5);
    echo "</pre>";

    fwrite($fg, $parts5);
} 

fclose($fg);
fclose($fh);

but new problem arises with this code where i get double or triple on the floating points after the match.

$thisisit[] = "";
$thisisit2[] = "";
$countThis = 0;
$fh = fopen("sometext.txt", "r");
$new_array = ""; $parts = "";
while (!feof($fh)) {
    $line = fgets($fh);
    $line2 = $line;
    $newword = "";
    $match1 ="/^\s*?[\d]+,[\d]+/";
    $parts1 = preg_match($match1, $line, $regs);
    foreach($regs as $key => $lame) {
        $thisisit[] = $lame;
    }
    $match2 ="/(?:(\d{1,3},\d{1,3},))(\d{1,3},\d{1,3},\d{1,3},[01][\.]?[\d]*)/";
    $parts2 = preg_match($match2, $line2, $regs2);
    foreach($regs2 as $key2 => $lame2) {
        $thisisit2[] =$lame2;
    }
    $countLame = count($thisisit);
} 

echo "</script>";
$newCounter = 0;
for($i = 0; $i < (500); $i++) {
    echo  $thisisit[$i] . "<br />";
    echo $thisisit2[$newCounter] . "<br />" ;
    $newCounter = $newCounter +4;
}
fclose($fh);

here is some text in the text file i am working on

 42,23,162,166,48,0.156863
 43,23,162,166,47,0.156863
 44,23,167,170,67,0.219608
 45,23,162,166,47,0.156863
 46,23,167,170,67,0.219608
 47,23,162,166,37,0.117647
 48,23,162,167,32,0.0941176
                                      86,23,163,167,40,0.12549
 87,23,160,164,47,0.164706
 88,23,188,190,122,0.352941
 86,24,233,234,197,0.486275
 87,24,251,250,250,1
 88,24,251,250,250,1
 89,24,251,250,250,1
  • 1
    It's simple: use `(\d+)` for capturing numbers, note `,` and `:` literally, `\s*` for (optional) spaces, escape `\(` and `\)` and you're done. – mario Oct 03 '12 at 20:08
  • * See also [Open source RegexBuddy alternatives](http://stackoverflow.com/questions/89718/is-there) and [Online regex testing](http://stackoverflow.com/questions/32282/regex-testing) for some helpful tools, or [RegExp.info](http://regular-expressions.info/) for a nicer tutorial. – mario Oct 03 '12 at 20:09
  • `\s` isn't just for spaces. It also matches newlines, carriage returns, tabs, and spaces (`[\n\r\t ]`). – Sly Oct 03 '12 at 20:31

3 Answers3

0
> '0,0: (255,255,255,  0)'.match(/^([^:]+) *: *\(([^)]+)\)/)
[ '0,0: (255,255,255,  0)',
  '0,0',
  '255,255,255,  0',
  index: 0,
  input: '0,0: (255,255,255,  0)' ]
Sly
  • 1,145
  • 7
  • 19
0
/^(\d+,\d+):.*\(([^)]*)\)$/

This should pull down the coordinates into capture group 1, and the RGBA values into group 2. Note, though, that it's probably more efficient to use string manipulation for this, if that matters to you. You can split the string on : to get the coordinates, then split on srgba( and chop off the last character for the color values.

Justin Morgan - On strike
  • 30,035
  • 12
  • 80
  • 104
0

Example #1:

If you have multiple lines $input

Code

$input = '
    0,0: (255,255,255,  0)  #FFFFFF00  srgba(255,255,255,0)
    43,23: (162,166, 47, 40)  #A2A62F28  srgba(162,166,47,0.156863)
';
preg_match_all('~^\s*(.+?):.+srgba\((.+?)\)\s*$~m', $input, $match);
print_r($match);

Output

Array
(
    [0] => Array
        (
            [0] => 0,0: (255,255,255,  0)  #FFFFFF00  srgba(255,255,255,0)
            [1] => 43,23: (162,166, 47, 40)  #A2A62F28  srgba(162,166,47,0.156863)
        )
    [1] => Array
        (
            [0] => 0,0
            [1] => 43,23
        )
    [2] => Array
        (
            [0] => 255,255,255,0
            [1] => 162,166,47,0.156863
        )
)

Example #2:

If you have single line $input with deeper regex:

Code

$input = '43,23: (162,166, 47, 40)  #A2A62F28  srgba(162,166,47,0.156863)';
preg_match('~^\s*(\d+),\s*(\d+):.+srgba\(\s*(\d+),\s*(\d+),\s*(\d+),\s*(.+?)\)\s*$~m', $input, $match);
print_r($match);

Output

Array
(
    [0] => 43,23: (162,166, 47, 40)  #A2A62F28  srgba(162,166,47,0.156863)
    [1] => 43
    [2] => 23
    [3] => 162
    [4] => 166
    [5] => 47
    [6] => 0.156863
)
Community
  • 1
  • 1
Glavić
  • 42,781
  • 13
  • 77
  • 107