• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Replace text by HTML links

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, sorry for my poor english

I need to replace any valid URL (Example: "https://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
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried to remove all the invalid escape sequences:



But when I do:



I get the following error:

 
Marshal
Posts: 27366
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
^ marks the beginning of a string literal, $ marks the end. Now do the math ;)
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 377
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22682
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I've been selected to go to the moon! All thanks to this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic