lundi 12 août 2019

How to use Pattern in Java?

Good evening dear congregation!

I would like to transfer the following code from Python to Java:

import re

payload = re.search(
    r'decrypt\.setPrivateKey\("(?P<privateKey>[^"]+)".*?'
    r'decrypt\.decrypt\("(?P<cryptText>[^"]+)".*?'
    r'document\.cookie="ipp_uid=(?P<ipp_uid>[^"]+)".*?'
    r'document\.cookie="ipp_uid1=(?P<ipp_uid1>[^"]+)".*?'
    r'document\.cookie="ipp_uid2=(?P<ipp_uid2>[^"]+)".*?'
    r'url\s\+=\s"(?P<makeURL>.*?)"\;.*?'
    r'salt="(?P<salt>[^"]+)"',
    ret.content.decode('utf-8'),
    re.MULTILINE | re.DOTALL
)

I have already tried the following code:

String patternString = "r'decrypt\\.setPrivateKey\\(\"(?P<privateKey>[^\"]+)\".*?'\n"
                    + "    r'decrypt\\.decrypt\\(\"(?P<cryptText>[^\"]+)\".*?'\n"
                    + "    r'document\\.cookie=\"ipp_uid=(?P<ipp_uid>[^\"]+)\".*?'\n"
                    + "    r'document\\.cookie=\"ipp_uid1=(?P<ipp_uid1>[^\"]+)\".*?'\n"
                    + "    r'document\\.cookie=\"ipp_uid2=(?P<ipp_uid2>[^\"]+)\".*?'\n"
                    + "    r'url\\s\\+=\\s\"(?P<makeURL>.*?)\"\\;.*?'\n"
                    + "    r'salt=\"(?P<salt>[^\"]+)\"'";
            Pattern payload = Pattern.compile(patternString);
            String content = new String(html.getBytes(), "UTF-8");
            Matcher m = payload.matcher(html);
            if(m.find()){
                System.out.println("Found: " + m.group(0));
            }else{
                System.out.println("not found");
            }

... but I am getting this error:

java.util.regex.PatternSyntaxException: Unknown inline modifier near index 29
r'decrypt\.setPrivateKey\("(?P<privateKey>[^"]+)".*?'
    r'decrypt\.decrypt\("(?P<cryptText>[^"]+)".*?'
    r'document\.cookie="ipp_uid=(?P<ipp_uid>[^"]+)".*?'
    r'document\.cookie="ipp_uid1=(?P<ipp_uid1>[^"]+)".*?'
    r'document\.cookie="ipp_uid2=(?P<ipp_uid2>[^"]+)".*?'
    r'url\s\+=\s"(?P<makeURL>.*?)"\;.*?'
    r'salt="(?P<salt>[^"]+)"'
                             ^
    at java.util.regex.Pattern.error(Pattern.java:1957)
    at java.util.regex.Pattern.group0(Pattern.java:2896)
    at java.util.regex.Pattern.sequence(Pattern.java:2053)
    at java.util.regex.Pattern.expr(Pattern.java:1998)
    at java.util.regex.Pattern.compile(Pattern.java:1698)
    at java.util.regex.Pattern.<init>(Pattern.java:1351)
    at java.util.regex.Pattern.compile(Pattern.java:1028)
    at fabian.site.MyModule.test(MyModule.java:76)
    at fabian.site.MyModule.run(MyModule.java:61)
    at fabian.thread.ThreadPool$PoolThread.run(ThreadPool.java:50)

Thank you for your help guys!

Aucun commentaire:

Enregistrer un commentaire