• 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
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Remove special characters/regular expression from string.

 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to remove special character from the web address. Following is my code.



But i am getting the same <http://www.semanticweb.org/ontologies/8/untitled-ontology-34#Manganese>; in output.
My desired output is Manganese is what i want to display. Please tell me where i am wrong.
 
Rancher
Posts: 2140
15
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Marshal
Posts: 66189
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Incomplete suggestion:-
 
Campbell Ritchie
Marshal
Posts: 66189
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swastik's suggestion is better than mine.
 
Bartender
Posts: 2402
106
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like swastik's solution too. Here's what I thought using regex :

 
Sheriff
Posts: 6363
172
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm, I don't like ".*#(.*)", how about [^#]*#(.*)
 
salvin francis
Bartender
Posts: 2402
106
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:...how about [^#]*#(.*)


Gives a different output. I think OP wants the last occurrence of # followed by the word. My regex prints "ranch" for "I#love#code#ranch" and your regex prints "love#code#ranch"
 
salvin francis
Bartender
Posts: 2402
106
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here's my complete regex if we want to include the ">" too:
 
Saloon Keeper
Posts: 2754
359
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

salvin francis wrote:


I would make a slight change just because I dislike backslashes:
 
Gayathri Gayu
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Swastik Dey wrote:



Thanks for the help. One more question. I tried for the following code <http://www.semanticweb.org/ontologies/untitled-ontology-34#alloyAlsoKnownAs>; <http://www.w3.org/2000/01/rdf-schema#range>; <http://www.semanticweb.org/ontologies/untitled-ontology-34#DesignationType>;
I thought it will print alloyAlsoKnownAs range DesignationType But only the last destignation type is getting printed. I have more link address and I want to print the last name alone. How to do? Please help me.
 
Swastik Dey
Rancher
Posts: 2140
15
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post your code.
 
Gayathri Gayu
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't modified much.

Instead of one link i added 3 more links. Even I would like to add more link.
 
Swastik Dey
Rancher
Posts: 2140
15
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Gayathri Gayu
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IS this the only way to do? Because i have saved those link address in a text file and reading those in string. As i have 60-80 links like this.
 
Swastik Dey
Rancher
Posts: 2140
15
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When reading from text file, instead of reading them in a single string, read each line and and set it in different strings.
 
Gayathri Gayu
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How will i do this? Is it like i have to store them in array and the read line by line?
 
Swastik Dey
Rancher
Posts: 2140
15
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Show how have stored the data in the text file, and how you are reading it.
 
Gayathri Gayu
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am attaching the output.txt file for your reference



I couldn't attach text file so i am pasting text file contents here below. Either way i am trying to display the mentioned output. It was owl file i am converting to string and trying to remove special characters. Else i am saving the raw output in txt file and trying to achive my desired output

 
Swastik Dey
Rancher
Posts: 2140
15
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Gayathri Gayu
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code partially works. I am getting the output as Not like

alloyAlsoKnownAs, range, DesignationType



Also can i able to replace replaceString to target.toString()?
 
Swastik Dey
Rancher
Posts: 2140
15
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Gayathri Gayu
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can i able to change the string to target.string()?
I tried changing and i got the following error java.lang.StringIndexOutOfBoundsException: String index out of range: -136889
 
Swastik Dey
Rancher
Posts: 2140
15
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Didn't get you very clearly, and how about the above solution did that work?
 
Gayathri Gayu
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Swastik Dey wrote:Didn't get you very clearly, and how about the above solution did that work?

Yes that worked perfectly fine. I have edited my above reply for your clear view.
 
Swastik Dey
Rancher
Posts: 2140
15
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is target variable?  What value do you have there?
 
Gayathri Gayu
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following is my entire code.



My output of or target.toString() value

resp.getWriter().write("Hello World!!"+target.toString());

seems like




More than 200 lines like above is the value of target.toString().

 
Swastik Dey
Rancher
Posts: 2140
15
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this once

 
Gayathri Gayu
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code works fine. Now its just displaying them as text, But as those are links i want to display them as link itself. Is it possible?
 
Swastik Dey
Rancher
Posts: 2140
15
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry but your requirement is bit confusing.  You wanted to extract the text after #, right?  Now you want this text as link, so where will this link point to?
 
Gayathri Gayu
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As those links are hyperlinks like classes subclass etc, i would like to remove the uri's and display them as hyperlinks.
 
Swastik Dey
Rancher
Posts: 2140
15
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show a sample text how is it supposed to look.
 
Gayathri Gayu
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will be like if Steel is an element it will have few compositions of elements that will come under steel. And here steel will be the hyperlink. When clicking steel it will display its components. Hope i explained correctly
 
Swastik Dey
Rancher
Posts: 2140
15
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So is this a web app and when user clicks on Steel it opens another web page? Is this what you are trying to say?
 
Gayathri Gayu
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes you are correct.
 
Swastik Dey
Rancher
Posts: 2140
15
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the servlet code?
 
Gayathri Gayu
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am pasting the code for your reference.


in the inserted code you helped mein removing uri's and display in text
 
Swastik Dey
Rancher
Posts: 2140
15
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that, but I wanted to see how you are doing it in servlet?  I know the code will be more or less same but it would help if you show the exact code.
 
Gayathri Gayu
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exact code withour removing uri's will look like

This will display the output as raw content of the file.
 
Swastik Dey
Rancher
Posts: 2140
15
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok one more query.  Each link must be pointing to a particular page.  For e.g for link Steel it's must be pointing to a html/jsp page what's that?
 
Gayathri Gayu
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The file content is like

I am not sure where it will go in. I am trying to figure out that too
 
It's a beautiful day in this neighborhood - Fred Rogers. Tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!