• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

find a word in a text and take a part of a text

 
Dim Dioannou
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to find a word in a text file and then to take a part of it and to
save it in a String.
for example I have this part of text
<rdf:RDF
xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'

and when I find the word ''xmlns:'' I want to take the word after : till =
(rdf in this case) and to put it in a string.

Any idea how to do it?
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is the text always going to have that same pattern of "xmlns:"? If you you could use regular expressions to find this pretty easily.

Hunter
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Your question sounds a lot like this one that was asked yesterday. I posted an answer there that might be useful to you.
 
Harlin Seritt
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do something like this in a tight:

String text = "<rdf:RDF
xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'";

String del1 = "xmlns:";

String newString = text.split(del1)[1].split("=")[0];

System.out.println(newString);

But a regular expression would be the way to go.
 
kumarjit banerjee
Ranch Hand
Posts: 32
Eclipse IDE Oracle Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dim Dioannou wrote:I want to find a word in a text file and then to take a part of it and to
save it in a String.
for example I have this part of text
<rdf:RDF
xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'

and when I find the word ''xmlns:'' I want to take the word after : till =
(rdf in this case) and to put it in a string.

Any idea how to do it?



You may try this.

 
Dim Dioannou
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@kumarjit banerjee thank you
this is what i need

but can you explain the code in line 38, please?
 
kumarjit banerjee
Ranch Hand
Posts: 32
Eclipse IDE Oracle Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dim Dioannou wrote:@kumarjit banerjee thank you
this is what i need

but can you explain the code in line 38, please?



Hi


as.add(String.copyValueOf(m.group().toCharArray(), 6, m.group().length()-7));


This line copies the matched string (m.group returns the total matched string) from the characters of at position 6 to total length-7, this because

xmlns: length=6
= length=1

So the total length =7

Hence the code.
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All the approaches suggested so far are horribly brittle. It's perfectly possible for the XML to change in various valid ways that would break your code. Regexps simply are not capable of dealing with that. If you're handling XML, you need to use XML/DOM APIs.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic