• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Replace text by HTML links

 
Elías Turbay
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, sorry for my poor english

I need to replace any valid URL (Example: "http://www.coderanch.com") with an HTML link,

I searched all over the net and I could not find the way to go generic.

I understand that I have to use regular expressions, but I have no experience with them.

Thanks in advance
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Weird, the first result on google is this: http://geekswithblogs.net/casualjim/archive/2005/12/01/61722.aspx
 
Elías Turbay
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sebastian

When I try to use it:



I get the following errors :

Multiple markers at this line
- Syntax error on tokens, delete these tokens
- Syntax error on tokens, delete these tokens
- Syntax error, insert "AssignmentOperator Expression" to complete
Expression
- Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )
- Syntax error, insert ";" to complete BlockStatements
- Syntax error on tokens, delete these tokens
- The left-hand side of an assignment must be a variable

Can you help me with this?
 
Elías Turbay
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to remove all the invalid escape sequences:



But when I do:



I get the following error:

 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you see a regex string (or any string, really) which contains backslashes, and you want to make a Java string literal out of it, you have to replace each \ by \\.
 
Elías Turbay
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tanks Paul.

Now I have this code, and it works fine...




But I need another thing and I don't know how to do it. For example:



And the result should be:

 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
^ marks the beginning of a string literal, $ marks the end. Now do the math ;)
 
Henry Wong
author
Marshal
Pie
Posts: 21518
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand that I have to use regular expressions, but I have no experience with them.


I am really not a fan of this. IMO, if you don't have experience with regex, you should not start with complex regexes. Using code that you don't understand is never a good idea.

Get up to speed on regex. Use it for simple stuff til you get more comfortable. And in the meantime, I recommend that you just use the regular string methods such as substring(), charAt(), etc. You can always port it to regex later once you get better at it.

Henry
 
Sege Stephen
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Elías Turbay wrote:Tanks Paul.

Now I have this code, and it works fine...




But I need another thing and I don't know how to do it. For example:



And the result should be:



Did you find a solution? Please post it, I want to solve the same problem to be displayed in a chat window
 
Sean Clark
Rancher
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
^ marks the beginning of a string literal, $ marks the end. Now do the math ;)


Sebastian above give a big hint on how to solve this.

Please read the posts above.

Sean
 
Rob Spoor
Sheriff
Pie
Posts: 20671
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to try and find a copy of "Mastering Regular Expressions" by Jeff Friedl, as it handles this exact problem as an example. Perhaps you can find it at your local library.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic