Arun Shankar
Arun Shankar

Reputation: 2593

java equivalent of python regex

I am new to android development. And now i am struck with a regex pattern. I had tried many things but in vain.

I am trying to find the java equivalent of the regular expression "r'\^\d+\~[A-Za-z~ ]+'" which is written in python.

Thanks in advance

Edit:

Actually I want to parse the string :

"0~XYZ~Xamp Vampire~XMP~Vampire Cenet~~2013-7-9-16-39-25~~~~^12345~VAMPIRE CITY~Vampire City~VMC~Vampire Center~VPC~Xamp Center~XMPC~Xamper VC~XMVX~07.50~09.45~01.55~1111111~~~~~~~~0001001000~~~~~~~~~~~MAIL_EXPRESS~4640~1~0~0~2013-07-08~2018-07-08~84~43~MAIL_EXPRESS:84:1085,1085,675,0,575,0:650,650,415,315,355,965:460,460,295,260,250,720:245,245,165,100,145,345:280,280,190,0,165,0:135,135,90,90,80,170:55,45,40,10,35,45:0,0,0,0,0,0:40,30,40,0,40,40~0~0~~60~1~6303~~~Intercity Express~~1~SR~~BG~~~10010~,,En:,,GS:,,GSLRD:,,GS:,,2S:,,2S:,,2S:,,2S:D,D1,2S:C,C1,CC:,,2S:,,2S:,,2S:,,2S:,,2S:,,2S:,,2S:,,2S:,,2S:,,GSLR:~0~"

I wrote the regex in python as reg1=re.compile(r'\^\d+\~[A-Za-z~ ]+') and it gives me a result as ['^12345~VAMPIRE CITY~Vampire City~VMC~Vampire Center~VPC~Xamp Center~XMPC~Xamper VC~XMVX~'] as an array. I just want to reproduce this in java. I tried many combinations and what npinti suggests, but failed. Please help.

Upvotes: 1

Views: 707

Answers (3)

Arun Shankar
Arun Shankar

Reputation: 2593

After three hours of hunt (since am a beginner in android), I find the correct thing, finally!

String myRegex = "[\\^]\\d{5}~[A-Za-z~ ]+";

This gives me what I need. Thanks to all those who tried to help me :-)

Upvotes: 0

npinti
npinti

Reputation: 52185

This should work: r'\\^\\d+~[A-Za-z~ ]+. It should match an r followed by a ', which is then followed by a ^, one or more digits followed by a ~ and one or more repetitions of letters, ~ and a white space.

In java the \ is a special character, so it needs to be escaped, this is why we have the extra \ in front of the \ required by the regex engine.

EDIT: I was unable to find python regular expressions starting with r', so I am assuming that you need to match that.

If this is not the case, just use \\^\\d+~[A-Za-z~ ]+.

EDIT:

This code seems to be working:

        String file = "0~XYZ~Xamp Vampire~XMP~Vampire Cenet~~2013-7-9-16-39-25~~~~^12345~VAMPIRE CITY~Vampire City~VMC~Vampire Center~VPC~Xamp Center~XMPC~Xamper VC~XMVX~07.50~09.45~01.55~1111111~~~~~~~~0001001000~~~~~~~~~~~MAIL_EXPRESS~4640~1~0~0~2013-07-08~2018-07-08~84~43~MAIL_EXPRESS:84:1085,1085,675,0,575,0:650,650,415,315,355,965:460,460,295,260,250,720:245,245,165,100,145,345:280,280,190,0,165,0:135,135,90,90,80,170:55,45,40,10,35,45:0,0,0,0,0,0:40,30,40,0,40,40~0~0~~60~1~6303~~~Intercity Express~~1~SR~~BG~~~10010~,,En:,,GS:,,GSLRD:,,GS:,,2S:,,2S:,,2S:,,2S:D,D1,2S:C,C1,CC:,,2S:,,2S:,,2S:,,2S:,,2S:,,2S:,,2S:,,2S:,,2S:,,GSLR:~0~";
        Pattern p = Pattern.compile("\\^\\d+\\~[A-Za-z~ ]+");
        Matcher m = p.matcher(file);
        if(m.find())
        {
            System.out.println(m.group(0));
        }
        System.out.println("Finished");

Yields:

^12345~VAMPIRE CITY~Vampire City~VMC~Vampire Center~VPC~Xamp Center~XMPC~Xamper VC~XMVX~
Finished

The whole r' thing seems to have given me some trouble and I actually omitted an initial \\ in my previous edit (which has now been fixed). I essentially used the same regular expression which @Kent suggested (so his should be the answer which should be accepted). I simply added some java code to better help you get what you wan :).

Upvotes: 2

Kent
Kent

Reputation: 195179

you just replace backslash with double-backslash. try this string (in java) as regex:

String myRegex = "\\^\\d+\\~[A-Za-z~ ]+";

Upvotes: 2

Related Questions