• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Can someone help me with a regular expression?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, im trying to convert a string like this

hello mylist liststart:1.one 2.this is point two 3.and here we have point three 4.whatever12345 5.:-) end:listend rest of the line

into some valid html code (html list). when im finished it should look like this:

<ol start="1" type="1">
<li>one</li>
<li>this is point two</li>
<li>and here we have point three</li>
<li>whatever12345</li>
<li>:-)</li>
</ol>

here is what i have so far:





but this code doenst work correctly. anyone any idea?

thanks in advance :-)
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Katja Ernst:
hi, im trying to convert a string like this

hello mylist liststart:1.one 2.this is point two 3.and here we have point three 4.whatever12345 5.:-) end:listend rest of the line

into some valid html code (html list). when im finished it should look like this:

<ol start="1" type="1">
<li>one</li>
<li>this is point two</li>
<li>and here we have point three</li>
<li>whatever12345</li>
<li>:-)</li>
</ol>

here is what i have so far:





but this code doenst work correctly. anyone any idea?

thanks in advance :-)



Hi Katja,

I wouldn't do this with a regular expression. I think it's better handled by a parser class that would take a String input and turn it into the appropriate tokens. I'd have a toXML() method that would return the tokens as the XML stream that you want: liststart, liststart, and a delimited string in-between. Choose the delimiter carefully - "dot" might not be the best choice if any of the data can sensibly contain a period.
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:
 
Katja Ernst
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, thanks to both of you.

@alan: your version will not put the last point (5. ...) into <li></li>. what must i do to correct this?

thanks :-)
 
Alan Moore
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works for me, but the regex does assume there's exactly one space character following each list entry. Maybe you need something more flexible:
 
Katja Ernst
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, thanks. if i use a line like this one here:

String s = "hello list liststart:1.:-) 2.yyy two 3.dsdsds 4.ttt 5.five:listend and end of the string.";


the output is:

hello list <ol start=1 type=1><li>:-)</li>
<li>yyy two</li>
<li>dsdsds</li>
<li>ttt</li>
5.five</ol> and end of the string.

but should be:

hello list <ol start=1 type=1><li>:-)</li>
<li>yyy two</li>
<li>dsdsds</li>
<li>ttt</li>
<li>five</li></ol> and end of the string.

any ideas?
 
Alan Moore
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that you changed the ending delimiter without telling me (but I agree that that "end" token didn't need to be there). Try this:
 
Katja Ernst
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, thanks :-D....works as it should! the end delimiter was no delimiter, it was just a word :-)...

thanks again
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic