Skip to content
Advertisement

Python Regex to Java

I am trying to convert a python regex to java. It finds a match in python but fails on the same string in java.

Python regex : "(CommandLineEventConsumer)(x00x00)(.*?)(x00)(.*?)({})(x00x00)?([^x00]*)?".format(event_consumer_name)

Java regex : "(CommandLineEventConsumer)(\u0000\u0000)(.*?)(\u0000)(.*?)(" + event_consumer_name + ")(\u0000\u0000)?([^\u0000]*)?"

I also tried this : "(CommandLineEventConsumer)(\x00\x00)(.*?)(\x00)(.*?)(" + event_consumer_name + ")(\x00\x00)?([^\x00]*)?"

What I’m I missing please?

I have attached a piece of the code

String sampleStr = "u0000u0000�u0003bu0000u0000u0000�u0005u0000u0000u0003u0000u0000�u0000u000Bu0000u0000u0000���u0005u0000u0000u0000u0003u0000u0000u0000 u0000u0000u0000u0000stringu0000u0000WMIDataIDu0000u0000SystemVersionu0000bu0000u0000u0000fu0000.u0000u0000u0000u0000u0000u0000u0000)u0000u0000u0000 u0000u0000�u0003bu0000u0000u0000'u0006u0000u0000u0003u0000u0000�u0000u000Bu0000u0000u0000��/u0006u0000u0000u0000u0003u0000u0000u0000u000Bu0000u0000u0000u0000stringu0000u0000WMIDataIDu0000fu0000u0000u0000u0000u0000u0000u0000u0000u0000u0000�u0016u0000u0000u0000Ru0000Ou0000Ou0000Tu0000\u0000Mu0000iu0000cu0000ru0000ou0000su0000ou0000fu0000tu0000\u0000Hu0000ou0000mu0000eu0000Nu0000eu0000tu0000u0019u0000u0000u0000Hu0000Nu0000eu0000tu0000_u0000Cu0000ou0000nu0000nu0000eu0000cu0000tu0000iu0000ou0000nu0000Pu0000ru0000ou0000pu0000eu0000ru0000tu0000iu0000eu0000su0000 u0000u0000u0000Cu0000ou0000nu0000nu0000eu0000cu0000tu0000iu0000ou0000nu0000�u0000u0000u0000Nu0000Su0000_u00005u00001u00001u00006u00002u00006u0000Fu0000Au0000Eu00004u0000Fu00005u00007u0000Du0000Bu0000Du00002u00000u0000Du0000Fu00005u0000Cu0000Du00004u00004u0000Au00004u00001u0000Du0000Au0000Eu0000Cu0000Eu0000Du00002u00008u0000Cu0000Fu00007u0000Bu00003u0000Fu0000Du00008u0000Bu00001u00002u00000u00001u00002u0000Cu00007u0000Fu00004u0000Bu00005u00008u0000Fu00004u00004u0000Eu00006u00006u00005u0000\u0000Ku0000Iu0000_u0000Au00000u00001u00000u00008u0000Cu0000Eu00002u00006u00001u0000Du00006u0000Cu0000Du00007u00000u0000Du00003u00005u00000u0000Fu00005u0000Bu00007u00002u0000Fu00002u0000Eu00009u00008u00007u00004u0000Au0000Eu00006u0000Eu00000u00000u00004u0000Du00003u00000u00002u00009u00000u00001u00005u0000Bu00000u00009u00001u00009u0000Bu00001u0000Bu0000Du00003u00002u00006u0000Bu0000Bu00006u00004u00009u0000\u0000Iu0000_u0000Eu0000Du0000Cu0000Eu0000Au00001u00004u0000Eu0000Cu00006u00003u0000Au00005u00007u00004u00001u0000Fu0000Au0000Au00006u00003u00000u00001u0000Cu00007u00007u0000Cu0000Au00002u00006u00000u0000Au0000Bu0000Eu0000Cu00000u0000Eu00007u00007u00000u00009u00005u00001u00004u0000Fu00006u0000Au00003u00002u0000Cu00000u00003u00004u00007u0000Eu00000u00002u00006u00008u00001u00007u0000Cu00008u00008u0000u0000u0000WQL:Re4u00007u0000Cu00007u00009u0000Eu00006u00002u0000Cu00002u00002u00002u00007u0000Eu0000Du0000Du00000u0000Fu0000Fu00002u00009u0000Bu0000Fu00004u00004u0000Du00008u00007u0000Fu00002u0000Fu0000Au0000Fu00009u0000Fu0000Eu0000Du0000Fu00006u00000u0000Au00001u00008u0000Du00009u0000Fu00008u00002u00005u00009u00007u00006u00000u00002u0000Bu0000Du00009u00005u0000Eu00002u00000u0000Bu0000Du00003u0000�3u�&��u0001����+u0004�u0001�u0000u0000u0000u0000u0000u0000u0000u0000u0000f;u0000u0000u0000u000Fu0000u0000u0000�u0000u0000u0000Fu0000u0000u0000/u0000u0000u0000u0000u0000u0000u0000u0004u0000u0000u0000u0001�u0000u0000�u0000__EventFilteru0000u001Cu0000u0000u0000u0001u0005u0000u0000u0000u0000u0000u0005u0015u0000u0000u0000�tw�}n" +
            "z�p�)��u0001u0000u0000u0000root\cimv2u0000u0000BVTFilteru0000u0000SELECT * FROM __InstanceModificationEvent WITHIN 60 WHERE TargetInstance ISA "Win32_Processor" AND TargetInstance.LoadPercentage > 99u0000u0000WQLu0000Bu0000Bu0000Fu0000Cu0000Cu0000Bu00004u00004u00004u0000Cu0000Fu00006u00006u0000Au0000Au00000u00009u0000Au0000Eu00006u0000Fu00001u00005u00009u00006u00007u0000Au00006u00008u00006u00005u00001u00007u00005u0000Bu0000Bu00000u0000Eu0000Du00002u00001u00006u0000Du00001u00009u00009u00007u00000u0000Au00007u00009u00008u00008u0000Bu00007u00002u0000Cu0000Du0000Fu00000u0000Au00003u0000Au00004u0000�3u�&��u0001Ԏ��+u0004�u0001�u0000u0000u0000u0000u0000u0000u0000u0000u000F�����"u0000u0000u0000u0000u0000u0000u0000u0000/u0000u0000u0000Ou0000u0000u0000u0000u0000u0000u0000u001Au0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000\u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0000u0004u0000u0000u0000u0001qu0000u0000�u0000CommandLineEventConsumeru0000u0000cscript KernCap.vbsu0000u001Cu0000u0000u0000u0001u0005u0000u0000u0000u0000u0000u0005u0015u0000u0000u0000�tw�}n" +
            "z�p�)��u0001u0000u0000u0000BVTConsumeru0000u0000C:\\tools\\kernrateu00000u0000Au00007u0000Au0000Bu0000Eu00006u00003u0000Fu00003u00006u0000Eu00002u0000Bu00002u00009u00002u00000u0000Fu0000Eu0000Du0000Au0000Fu0000Au0000Eu00008u00004u00009u00008u00002u00003u0000Au0000Fu00009u00004u00002u00009u0000Cu0000Cu00000u0000Eu0000Au00003u00007u00003u0000Fu0000Fu0000Eu0000Eu00001u00005u00000u00007u0000Eu0000Du0000Bu00002u00001u0000Fu0000Du00009u00001u00007u00000u0000�3u�&��u0001����+u0004�u0001�u0000u0000u0000u0000u0000u0000u0000u0000�";
    String event_consumer_name = "BVTConsumer";
    String cPattern = "(CommandLineEventConsumer)(\u0000\u0000)(.*?)(\u0000)(.*?)(" + event_consumer_name + ")(\u0000\u0000)?([^\u0000]*)?";

    Pattern consumer_mo = Pattern.compile(cPattern, Pattern.CASE_INSENSITIVE);
    Matcher consumer_match = consumer_mo.matcher(sampleStr);

    if(consumer_match.find()){
        System.out.println(consumer_match.group(6));
    }

UPDATE

In python the groups return

python result screenshot

Advertisement

Answer

From what I posted as comments: The (CommandLineEventConsumer)(u0000u0000)(.*?)(u0000)(.*?) part matches fine.
group(3) gets cscript KernCap.vbs
group(4) gets a null character
but group(5) gets nothing.

I did try in Python and I have the exact same lack of match when I include the (BVTConsumer). So you probably had a difference in the code doing the matching in Python, not the regex itself.

So the reason is that you have a n in your string so the matching stops there. If you do

Pattern consumer_mo = Pattern.compile(cPattern, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);

it does match in your example.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement