0

I have a search String which contains the format below:

Search String

    111651311
    111651303
    4111650024
    4360280062
    20167400

It needs to be matched with sequence of numbers below

001111651311000
001111651303000
054111650024000
054360280062000
201674000000000

Please note the search strings have been added with additional numbers either on each sides. I have tried the regex below in java to match the search strings but it only works for some.

Pattern pattern = Pattern.compile("([0-9])\1*"+c4MIDVal+"([0-9])\1*");

Any advice ?

Update

Added the code I used below might provide some clarity on what am trying to do

Code Snippet

public void compare(String fileNameAdded, String fileNameToBeAdded){
        
        List<String> midListAdded = readMID.readMIDAdded(fileNameAdded);
        
        HashMap<String, String> midPairsToBeAdded = readMID.readMIDToBeAdded(fileNameToBeAdded);
        List <String []> midCaptured = new ArrayList<String[]>();
        
        
        for (Map.Entry<String, String> entry: midPairsToBeAdded.entrySet()){
            
            String c4StoreKey = entry.getKey();
            String c4MIDVal = entry.getValue();
            
            Pattern pattern = Pattern.compile("([0-9]?)\\1*"+c4MIDVal+"([0-9]?)\\2*");
                        
            for (String mid : midListAdded){
                
                Matcher match = pattern.matcher(mid);
//              logger.info("Match Configured MID :: "+ mid+ " with Pattern "+"\\*"+match.toString()+"\\*");
                
                if (match.find()){
                    
                    midCaptured.add(new String []{ c4StoreKey +"-"+c4MIDVal, mid});
                    
                }
            }
        }
        
        logger.info(midCaptured.size()+ " List of Configured MIDs ");
        for (String [] entry: midCaptured){
            
            logger.info(entry[0]+ "- "+entry[1]  );
        }
    }
3
  • 2
    Wouldn't indexOf be sufficient ? Commented May 3, 2016 at 13:55
  • or contains. K.I.S.S. Commented May 3, 2016 at 14:02
  • i have included my code please check Commented May 4, 2016 at 7:13

2 Answers 2

2

You need to refer the second capturing group in the second part and also you need to make both the patterns inside the capturing group as optional.

Pattern pattern = Pattern.compile("([0-9]?)\\1*"+c4MIDVal+"([0-9]?)\\2*");

DEMO

Sign up to request clarification or add additional context in comments.

9 Comments

Also, the dynamic part should be quoted: Pattern.quote(c4MIDVal). Probably not needed, since it's just numbers, but still a good idea.
hi avinash, thanks for the reply. tried your suggestion but still have the number of results. It can match this but not these sequence 054111650024000.
@dimas what's the c4MIDVal value?
c4MIDVal is the search string
what's the value for this?
|
0

What is the problem by using the String.contains() method?

"001111651311000".contains("111651311"); // true
"201674000000000".contains("111651311"); // false

2 Comments

it won't return the matched string.
@AvinashRaj It is the String which you are comparing?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.