regex to detect file structure in java [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to detect the file structure in a string.
e.g
if I have a string as /name/test/testme/2 I should be able to store it in a arraylist as different elements like {[name],[test],[testme],[2]}

String[] elements = "/name/test/testme/2".split("/");
More info can be found in the String.split() Javadoc

As Lukas pointed out, (please give him some upvoting) you should use the split method.
String[] elements = "/name/test/testme/2".split("/");
The regular expressions are not used to split strings in sections. Regular expressions are used for matching the target string with a specific generic format. In this case a boolean value indicating if the strings match is returned.
Hope I helped!

Related

Assign information to element in array in java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Is it possible to assign information to an element in an array in java?
For example, can I have an element containing a title and a number etc?
How do you go about this? thanks
Yes, instead of having an array of primitive types (like ints) or Strings, define your own class and then create an array of that. Then you can have whatever information you want in there.
Alternatively, if the array must be an array of primitives or Strings because some other method requires it to be in that form, you could either create a second array like that on demand, or use a second array for the associated data (again you might create your own class for this).

Parsing using Jsoup [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I ma new to java.
I have a string s:
s="<name>header</name><content>Good Morning</content>"
How to get value of content and name using Jsoup?
What JSoup returns when content is empty?null?
You can use jsoup:
Connection con2=Jsoup.connect(url);
Document doc = con2.get();
//or use Document doc = Jsoup.parse(html);
Element e=doc.head().select("meta[name=header]").first();
String url=e.attr("content");
http://jsoup.org/cookbook/extracting-data/attributes-text-html
http://jsoup.org/cookbook/extracting-data/selector-syntax
For your edit I do agree with the answer that #Hein give you.
Use this RegEx for example: name=(.*) content=(.*) \/>. The name will be in the first group and the content in the second.
I would recommend double quotes around the strings though. In that case you can use this regex: name=\"(.*)\" content=\"(.*)\"
Edit after the OPs edit:
If you have complete control of the data yourself you should consider saving the name and content in seperate columns in your database, or look into serialization maybe.

How to get a substring from a string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have this xml file from where I'm reading this string,
http://localhost:8080/sdpapi/request/10/notes/611/
My question is how can I get just the 611, which is of variable, can be 100000, for example, from this string?
Split the string
String input = "http://localhost:8080/sdpapi/request/10/notes/611/";
String output = input.split("notes/")[1].split("/")[0];
output is the value you need
What language?
Anyway, in most cases it's a syntax like:
String.substring(begin, length);
... where 'begin' is the number of the letter in the string-1. For extracting http from the above string you would write
substring(0, 4);
In case you always need the last string between the last two '/'s, you can retrieve the position of the slashes with index-functions (as stated in the answer of #Liran for example).
// EDIT: In Java the second parameter of substring is not length, but endIndex:
String s = "http://localhost:8080/sdpapi/request/10/notes/611/";
s.substring(46, s.lastIndexOf('/'));
It depends on programming language you use, but Regular Expressions should be the same in most of them:
/(\d+)\/$/
well, it depend in what language are you writing... in c# for example
string s = #"http://localhost:8080/sdpapi/request/10/notes/611/";
s.SubString(s.LastIndexOf('/'));
or
Path.GetFileName(s);
for java
new File(s).getName();

Whitespace matching regex in Java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I am currently trying to make use of the java string function someString.replaceAll() to find commonly used words (and, the, by, of, etc) and replace them with " ". Based on the answers to the question at Whitespace Matching Regex - Java, I produced this function call:
data.replaceAll("(?i)\\sthe\\s", " ")
However, it isnt working and I'm really not sure why. Nothing about it looks wrong based on what I've found. Please help me!
Strings are immutable!
data = data.replaceAll("(?i)\\sthe\\s", " ");

Is it possible to use a String as the arguments for Files.move(source,target..) in Java? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Can you use strings in some way to define the source and target of files.move.
Heres the documentation http://docs.oracle.com/javase/tutorial/essential/io/move.html
According to the javadoc, you can not use strings as arguments for Files.move .
What seems to be a better solution for you, is using the rename method on File. Something like this:
File file = new File("/path/to/file/to/be/moved");
boolean moved = file.renameTo("/new/path/for/the/file");
if(!moved)
//Handle the error
Short answer is no: Files.move requires Path objects. That said, you can use Paths.get(str) to simply turn a String into a Path.

Categories

Resources