This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes split using metacharacters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "split using metacharacters" Watch "split using metacharacters" New topic
Author

split using metacharacters

Marko Debac
Ranch Hand

Joined: Aug 21, 2006
Posts: 121
Hi,
i have a question about regex and pattern matcher:

I need to split some string using regex;
lets say that dots in "sentence" represent lots of caracter beetwen



with these I got split on every "c", but I need to concatenate a fix prefix "b" to be a part of the splitter.

I need to have splitter exactly like this "b.. ..c". b and c are always fixed so we must use only them.

Thanks.
Arijit Daripa
Ranch Hand

Joined: Aug 09, 2008
Posts: 142
You are not so clear that everybody can get you. And I am very sorry to say that I couldn't get you.
Please write a demo INPUT and your desired OUTPUT. Like......



SCJP 5
Marko Debac
Ranch Hand

Joined: Aug 21, 2006
Posts: 121
String sentence = "a.. ..b.. ..c.. ..d.. ..e";

And at the end I need to have two strings given with split method:

String[] result = new String [];

result[0] // must be "a.. .."
result[1] // must be ".. ..d.. ..e"
// so as we see splitter must be "b.. ..c"
// I have trouble to define those dots between which can be a lots of various characters
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40

// I have trouble to define those dots between which can be a lots of various characters


Here is the issue. What is those dots supposed to represent? If they are dots, then you can split like with ... "b\\.\\. \\.\\.c". If they are to represent any number of any characters, then you can split like with ... "b.*?c". We need some more examples, which doesn't use dots to clarify.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Arijit Daripa
Ranch Hand

Joined: Aug 09, 2008
Posts: 142
Hey Dude here goes your class


Are you Online?
If yes, compile and acknowledge me whether you get the desired result.
Arijit Daripa
Ranch Hand

Joined: Aug 09, 2008
Posts: 142
OOPS! Henry has beaten me by 6 minutes. Great job Henry.
venkatesh badrinathan
Ranch Hand

Joined: Aug 03, 2008
Posts: 77
Thanks for everyone, but a very silly question out here,

tokens = sentence.split(paternSplitter);

how does the 'tokens' variable gets incremented everytime when a new token is found in the sentence???(we did not use any loop to mention it...)???
please explain


SCJP1.5
Marko Debac
Ranch Hand

Joined: Aug 21, 2006
Posts: 121
No, you didnt understand me; dots between b and c can representing lots of characters variable length; so it can be ..bsdgc.. or ..b vh tt 234 hjk c.. but b and c are always there and there are fixed.

I have tryed with this b\\w*c (in string) but it isnt..

thanks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40

I have tryed with this b\\w*c (in string) but it isnt..


I am wondering why didn't you try "b.*?c", which was recommended by at least two people on this thread.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: split using metacharacters
 
Similar Threads
How we can Count the no.of words in a given String
Splitting string around "." ?
Need regex to solve
inner class or new class or new method?
StringTokenizer I want to pass a String and tokenize