Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Regular Expression  RSS feed

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I need to match all the occurances of the following pattern in the given file--

db:id="db_value1"

Now thye challenge is that i want these value in the form of key-value pair.

For example, for above match--
key = db:id
value= db_value1 and so on.

Can you please help me out?


Thanks,
Kaya.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for that you don't necessarily need regular expressions... a split or StringTokenizer does just fine ...

 
kayanaat sidiqui
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Sebastian.
It was really a very good solution.

Thank you once again.
 
kayanaat sidiqui
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Now i am a bit stuck with the following pattern--


Search for anything in between --> <backUp> <\backUp>

Occurances of <backUp> <\backUp> may be more than one.

Please help me out.

Thanks
Kaya.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok there are various approaches.

the cleanest would be using an XMLParser such as SAXParser ...

you could also do it quick and dirty ...

one way would be using regular expressions
the other way is splitting again, which is probably the dirtiest solution of all but its the quickest and also the fastest regarding performance

so .




yap, thats about it, as i said, the quickest and dirtiest approach ;-)
 
kayanaat sidiqui
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for early response.
Well I tried the code with



But geeting exception



Why is this exception?

am i doing anything wrong?
 
Rancher
Posts: 42975
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

indexOf("</backUp>")


If the code is searching for a forward slash, then you shouldn't be surprised that none is found in a string that contains only backslashes.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey,

for it to be a valid xml style, you dont use backslashes but front-slashes ...
 
author
Marshal
Posts: 23437
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

kayanaat sidiqui wrote: Search for anything in between --> <backUp> <\backUp>

Occurances of <backUp> <\backUp> may be more than one.

Please help me out.

Thanks
Kaya.



One way to use regex is to use find() with the pattern "<backup>(.*?)</backup>"... in which case, group 1 will be the text between the backup tags. However, this will not work well if you have nested backup tags -- for complex XML, I would recommend using one of the 2 (or 3 for java 6) built in XML parsers.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!