• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regular Expression help  RSS feed

 
john mattucci
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a string that may contain a combination of urls and other information. All urls begin with http:// and if they separated by a blank if the string contains multiple items for example

String re = "test http://x.ca/abc/abcsurvey.studytaker.login this is test " +
"http://a.ca/abc/abcsurvey.adminteamweb.login my name is michael " +
"http://b.ca/abc/abcsurvey.localadmin.login ";

I would like to obtain it in this order
1/test
2/http://x.ca/abc/abcsurvey.studytaker.login
3/this is test
4/http://a.ca/abc/abcsurvey.adminteamweb.login
5/my name is michael
6/http://b.ca/abc/abcsurvey.localadmin.login

Pattern pattern = Pattern.compile("http://[^ ]+|.*? (?=http ");
Matcher matcher = pattern.matcher(s);
while(matcher.find()) {
System.out.println(matcher.group());

This works for some cases however I cannot get it to work for

String a = "test"

or

String b = "http://b.ca test";

or

String c = "test http://c.ca test";

Thank you all for your time. Sorry if I didnt post this in the right forum
 
Sergio Costa
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm, maybe that's too slick for a simple regExp. If you're feeling academic, check out javaCC. You'll have your parser going in an hour.

if you must do it youself, implement your own recursive left to right parser.

A= "^http://[^]+ "
B= !A
while rest of string != ""
find A | B
chop found from rest of string
iterate

if the entries have to be of type AB(AB)* use semaphores acordingly.
 
alex delaguardia
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you shoud try using a pattern, once you obtain a partial result use another pattern to catch the other strings.
 
Henry Wong
author
Sheriff
Posts: 23291
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I think that you need a third case. The third case being a minimum match of a least one character bounded by the end of line.

Henry
 
Alan Moore
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!