1

I have created one RegEx using online tool which runs on perl script, the following regex is as:

^BC1E_SENBR_IC601_FAPM_EN_ID(?:\.?\d\.?)(?:\.?\d\.?)(?:\.?\d\.?)(?:\.?\d\.?)_(?:\.?\w*\.?\.?\w*.?)_(?:\.?\w*\.?\.?\w*\.?).ASC$

the same i have coded to generate it from sourcePatternStr1 variable

which is completely matching the below match:

BC1E_SENBR_IC601_FAPM_EN_ID7777_zb0.9961821020403842np_zb0.9961821020403842np.ASC

the below is the following java program which is not giving expected results.

package com.att.tdms.test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test1 {

    public static void main(String[] args) {

        String sourceStr = "BC1E_SENBR_IC601_FAPM_EN_ID7777_zb0.9961821020403842np_zb0.9961821020403842np.ASC";
        String sourcePatternStr1 = "BC1E_SENBR_IC601_FAPM_EN_ID????_*_*.ASC";

        String destinPatternStr0 = "BC1E_SENBR_IC1601_FAPM_ID<1><2><3><4>_<5>_<6>.ASC";

        StringBuffer sourceRegEx = new StringBuffer(sourcePatternStr1);

        for(int i=sourcePatternStr1.length()-1; i>=0;i --){
            if(sourcePatternStr1.charAt(i) == '?'){

                sourceRegEx.replace(i, i+1, ("(?:\\\\.?\\\\d\\\\.?)"));
            }

            if(sourcePatternStr1.charAt(i) == '*'){

                sourceRegEx.replace(i, i+1, "(?:\\\\.?\\\\w*\\\\.?\\\\.?\\\\w*\\\\.?)");
            }
        }

        System.out.println(sourceRegEx); //BC1E_SENBR_IC601_FAPM_EN_ID(?:\\.?\\d\\.?)(?:\\.?\\d\\.?)(?:\\.?\\d\\.?)(?:\\.?\\d\\.?)_(?:\\.?\\w*\\.?\\.?\\w*\\.?)_(?:\\.?\\w*\\.?\\.?\\w*\\.?).ASC

        Matcher m9 = Pattern.compile(sourceRegEx.toString()).matcher(sourceStr);

        StringBuffer result2 = new StringBuffer(); // We'll build the updated copy here

        while (m9.find())
        {
            String matchedText = m9.group();
            int matchedFrom = m9.start();
            int matchedTo = m9.end();   
            System.out.println("matched [" + matchedText + "] " +
                    "from " + matchedFrom +
                    " to " + matchedTo + ".");
        }

    }

}

i am expecting out put as:

matched [7] from 26 to 28
matched [7] from 27 to 29
matched [7] from 29 to 30
matched [7] from 30 to 31
matched [7] from 26 to 28
matched [zb0.9961821020403842np] from 32 to 54
matched [zb0.9961821020403842np] from 55 to 75

can any one help me out, why is that not matching from java code where as it is working from perl script fine.

zb226
  • 9,586
  • 6
  • 49
  • 79
Mahboob Ali
  • 165
  • 8

0 Answers0