• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

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

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 32
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
The glass is neither half full or half empty. It is too big. But this tiny ad is just right:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic