如果使用分组元字符,则搜索和替换regexp会提供两个不同的输出

时间:2016-01-22 09:50:59

标签: regex perl sed

我从搜索获得不同的输出并替换perl中的regexp,具体取决于我是否使用原位替换(sed alternative)或常规搜索替换,还取决于我是使用$1还是──> cat test1.txt orig.avg.10 ──> cat test2.txt orig.avg.10 # EXPECTED ──> cat test1.txt | perl -lne '$_ =~ s/(avg\.[0-9]+)/$1\.vec/; print $_' orig.avg.10.vec # EXPECTED ──> cat test1.txt | perl -lne '$_ =~ s/(avg\.[0-9]+)/\1\.vec/; print $_' orig.avg.10.vec # EXPECTED ──> perl -p -i.bak -e "s/(avg\.[0-9]+)/\1\.vec/" test2.txt ──> cat test2.txt orig.avg.10.vec # UNEXPECTED ──> perl -p -i.bak -e "s/(avg\.[0-9]+)/$1\.vec/" test1.txt ──> cat test1.txt orig..vec

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <CL/cl.hpp>


#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_write.h"

int main() {

    unsigned int vectorSize = 1000;
    const std::string progCode = "__kernel                                   \n"
                            "void img_kernel( __read_only image2d_t inputImage, __write_only image2d_t outputImage) \n"
                            "{                                                                                             \n"
                            "const sampler_t sampler=CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP | CLK_FILTER_NEAREST; \n"
                            "    int width = get_image_width(inputImage);                                                      \n"
                            "    int height = get_image_height(inputImage);                                                    \n"
                            "    int2 pixelcoord = (int2) (get_global_id(0), get_global_id(1));                            \n"
                            "    float4 pixel = read_imagef(inputImage, sampler, pixelcoord);                         \n"
                            "    float color = (pixel.x + pixel.y + pixel.z)/3;                                            \n"
                            "    float4 outColor = (float4)(pixel.x,pixel.y,pixel.z, 1.0);                                    \n" 
                            "    write_imagef(outputImage, pixelcoord, outColor);          }                                     \n";
    int imageX, imageY, imageN;
    unsigned char *dataImage = stbi_load("test.jpg", &imageX, &imageY, &imageN, 3);
    if (dataImage == nullptr)
    {
        std::cout << "Unable to load picture" << std::endl;
        getchar();
        return 1;
    }

    cl_int  error;

    std::vector<cl::Platform> platformsList;
    error = cl::Platform::get(&platformsList);
    if (error != CL_SUCCESS) 
    {
        std::cout << "Unable to find any OpenCL platforms" << std::endl;
        getchar();
        return 1;
    }


    std::vector<cl::Device> devicesList;
    error = platformsList[0].getDevices(CL_DEVICE_TYPE_DEFAULT, &devicesList);
    if (error != CL_SUCCESS) 
    {
        std::cout << "Unable to find any OpenCL device" << std::endl;
        getchar();
        return 1;
    }

    cl::Device currentDevice = devicesList[0];
    std::string nameDevice, driverDevice;
    error = currentDevice.getInfo(CL_DEVICE_NAME, &nameDevice);
    error = currentDevice.getInfo(CL_DRIVER_VERSION, &driverDevice);
    std::cout << "Device : " << nameDevice << "   " << driverDevice << std::endl;;

    cl::Context context(currentDevice);

    cl::Program::Sources source;
    source.push_back({ progCode.c_str(), progCode.size() });
    cl::Program program(context, source);
    if (program.build({ currentDevice }) != CL_SUCCESS)
    {
        std::cout << " Error building: " << program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(currentDevice) << "\n";
        getchar();
        exit(1);
    }

    cl::ImageFormat globalImgFormat; 
    globalImgFormat.image_channel_data_type = CL_UNSIGNED_INT8;
    globalImgFormat.image_channel_order = CL_RGB;

    cl::size_t<3> origin;
    origin[0] = 0; origin[1] = 0, origin[2] = 0;
    cl::size_t<3> region;
    region[0] = imageX; region[1] = imageY; region[2] = 1;



    cl::Image2D inputImage(context, CL_MEM_READ_ONLY, globalImgFormat, imageX, imageY, 0, dataImage, &error);
    if (error != CL_SUCCESS) 
    {
        std::cout << "Unable to create cl Image for input." << std::endl;
        getchar();
        return 1;
    }

     cl::Image2D outputImage(context, CL_MEM_WRITE_ONLY, globalImgFormat, imageX, imageY, 0, nullptr, &error);
     if (error != CL_SUCCESS)
    {
        std::cout << "Unable to create cl Image for output." << std::endl;
        getchar();
        return 1;
    }

    cl::CommandQueue queue(context, currentDevice);


    cl::Kernel image_kernel(program, "img_kernel", &error);
    if (error != CL_SUCCESS)
    {
        std::cout << "Unable to create kernel." << std::endl;
        getchar();
        return 1;
    }

    error = image_kernel.setArg(0, inputImage);
    if (error != CL_SUCCESS)
    {
        std::cout << "Unable to set param." << std::endl;
        getchar();
        return 1;
    }
    error = image_kernel.setArg(1, outputImage);
    if (error != CL_SUCCESS)
    {
        std::cout << "Unable to set param." << std::endl;
        getchar();
        return 1;
    } 

    cl::NDRange globalSize(imageX, imageY);


    error = queue.enqueueNDRangeKernel(image_kernel, cl::NullRange, globalSize, cl::NullRange);
    if (error != CL_SUCCESS)
    {
        std::cout << "Unable to compute Image data." << std::endl;
        getchar();
        return 1;
    }
    queue.finish(); 



    unsigned char *resultPros = new unsigned char[imageX * imageY * imageN];
    error = queue.enqueueReadImage(outputImage, CL_TRUE, origin, region, 0, 0, resultPros);
    stbi_write_bmp("testresul.jpg", imageX, imageY, imageN, resultPros);


    stbi_image_free(dataImage);
    stbi_image_free(resultPros);
    getchar();
    return 0;
}

为什么会这样?

2 个答案:

答案 0 :(得分:5)

您正在使用"来包装您的perl代码,但这样做意味着shell可以并将插入$1。 请改用',一切都会按预期工作。

答案 1 :(得分:-1)

问题是我在#UNEXPECTED案例中使用了双引号,这使得扩展$1变量。有时需要在实现案例之前写下问题。