• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

split using metacharacters

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OOPS! Henry has beaten me by 6 minutes. Great job Henry.
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic