Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

split using metacharacters

 
Marko Debac
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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......

 
Marko Debac
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21506
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// 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
 
Arijit Daripa
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OOPS! Henry has beaten me by 6 minutes. Great job Henry.
 
venkatesh badrinathan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Marko Debac
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21506
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic