• Post Reply Bookmark Topic Watch Topic
  • New Topic

string parsing  RSS feed

 
Jigar Gosar
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i got a big problem , but i think , someone has alredy found a solution to it, so i thought to search for it, but i didnt find any, so i m posting it here,

Problem 1:

String st="my name is [*name*] [*surname*]. "
Hashtable ht=new Hashtable();
ht.add("name","Jigar");
ht.add("surname","Gosar");

now i call a method,

String processString(String st,hashTable ht);

now i want this method to return "my name is Jigar Gosar."
is there any java API that will do this for me , or provide me with some funcitionaly to do it, or is there any standard way of achieving it. ofcource i could use String.indexOf() methods and do the parsing, but i m not looking at reinventing the wheel, so plese help.

Problem 2:
String st="remove, [*begin*]..,unwanted ,chars.,. [*end*]. "

String processString(String st);

i wnat this func to return a string like "remove, unwanted chars",
u see all comas and fullstops between [*begin*] [*end*] are removed, so now i want a parser or tokinizer which will return tokenize my string on delimiters like [*begin*] , [*end*] and other such custom tokens.

so that i can also implement functionality like say,
"name=[*name*] \n [*if surname*] surname=[*surname*] [*endif surname*]"

so if i pass a hashtable containing only following (refering to method in problem 1)
ht.add("name","Jigar");
then "name=Jigar" is returned


and if i pass:
ht.add("name","Jigar");
ht.add("surname","Gosar");

then returned string is "name=Jigar surname=Gosar";

i hope u get my problem, are there any generic parsers or tokenizers that will hel me solve this problem,

please help me, i m in a fix

thanks,
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you looked into using java.text.MessageFormat?
 
Jigar Gosar
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
message format is good but my requirements are different, the argument placeholders in mesage format are like {0},{1}, {2}, i want them to be discriptive sucha as {*name*}, {*lastname*}, ..etc. also i want to implemant if condition and loops too, i dont mind implementing function like message format, but in order to parse the input string, are there an other helper classes that i can use?

also i could use any help in designing such a code from ground up.

thanks.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look into Regular Expressions. There are zillions of tutorials on the web, and a good starting point in Java is the JavaDoc for Pattern. Learning to love regular expressions is a non-trivial journey, but you've already found the best place on the planet to ask questions.

If you go this way: Make a pattern that describes your substitution syntax, like "find {* followed by any letters and numbers followed by *}". Use Pattern with Matcher and the find() method. The bit you matched, say {*FirstName*}, will be one of the "groups" captured by the Matcher. You can get the real first name from your Map and use Matcher's replace to put it into the string.

Regular expressions are surely worth learning some time. Might as well be now!

Oops forgot to say: RegEx was new in 1.4. If you're in 1.3 or earlier you can find Java RegEx packages that will work but they don't come with the JDK.
[ October 28, 2004: Message edited by: Stan James ]
 
Jigar Gosar
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks dude,

i'll try and learn regular expressions,

i m planning to use RE bundled with JDK.

so i think officia java tutorial will be the best place to start.

but tell me are there different types of RE, which one is supported by JDK1.4,

and is there any good, link, where i could begin learning RE, (compatible with those provided in JDK 1.4)


thanks.
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jigar,

is there any good, link, where i could begin learning RE

You can begin by reading Sun Java Tutorial: Regular Expressions. I found it useful when I started learning regular expression.

Joyce
[ October 31, 2004: Message edited by: Joyce Lee ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Grab an interactive expression evaluator somewhere, too. There's one that plugs into Eclipse (forget the name) and I sometimes play with "the RegEx Coach" to work things out.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!