1

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.

3 Answers 3

2

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

String myRegex = "\\^\\d+\\~[A-Za-z~ ]+";
Sign up to request clarification or add additional context in comments.

1 Comment

I can't vote you up since i have lesser reputation, but thank you very much
2

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 :).

8 Comments

I believe the r is not a part of regex in python. :)
@Kent: I was unable to find any example where the r' is used, so I assumed that the OP wanted to match that as well. If this is not the case, then I ammended for that as well, which is essentially what you posted.
It is ok. you can find it here:docs.python.org/2/reference/… Actually, OP confuses us a bit. He said a regex in py. I believe he meant r'\^\d+\~[A-Za-z~ ]+' but he quoted the whole thing again. It could mislead people to think the text between the double quotes is the regex in python...
@Kent I understand. If this is the case then your answer is correct. Upvote from me :).
r is for Raw-string - not a regex part :-)
|
0

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 :-)

Comments

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.