aspose file tools*
The moose likes Java in General and the fly likes Replace text by HTML links Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Replace text by HTML links" Watch "Replace text by HTML links" New topic
Author

Replace text by HTML links

Elías Turbay
Ranch Hand

Joined: Oct 06, 2005
Posts: 35
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

Joined: Feb 23, 2009
Posts: 1183
Weird, the first result on google is this: http://geekswithblogs.net/casualjim/archive/2005/12/01/61722.aspx


JDBCSupport - An easy to use, light-weight JDBC framework -
Elías Turbay
Ranch Hand

Joined: Oct 06, 2005
Posts: 35
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

Joined: Oct 06, 2005
Posts: 35
I tried to remove all the invalid escape sequences:



But when I do:



I get the following error:

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18563
    
    8

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

Joined: Oct 06, 2005
Posts: 35
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

Joined: Feb 23, 2009
Posts: 1183
^ marks the beginning of a string literal, $ marks the end. Now do the math ;)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18821
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Sege Stephen
Ranch Hand

Joined: Oct 06, 2007
Posts: 51
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

Joined: Jul 15, 2009
Posts: 377

^ 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


I love this place!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19692
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Replace text by HTML links