|
| |||||||||
| Tags: datatype, extract, int, java, programming language, string |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| Extract the elements of string in java
Hello, I have a string long enough my aim is to extract from string the elements that constitute it, but it is a little more special. Quote:
will be as follows Quote:
Last edited by Ash maker : 13-01-2010 at 01:52 PM. |
|
#2
| ||||
| ||||
| Re: Extract the elements of string in java
Hi, The best is probably construct a parser for it, I advise you to use Antlr (nothing to do with Ant). Basically you give him your "grammar": a variable is an alphanumeric string, an addition is: Variable '+' variable. Hope this will help you. If you have any more problems then do post back.
__________________ The FIFA Manager 2009 PC Game |
|
#3
| ||||
| ||||
| Re: Extract the elements of string in java
Hello, Even simpler, with RegExp and method String.split. You can try the following line of code. This will make your code simpler. Code: String[] res = s.split("\\+|\\*|/"); |
|
#4
| |||
| |||
| Re: Extract the elements of string in java
Hello I tried with splits, though I am not sure that this works correctly and is this the correct way to do it? Quote:
Quote:
|
|
#5
| ||||
| ||||
| Re: Extract the elements of string in java
Hello, The solution is to take the problem in reverse: instead of cutting the string according to certain criteria, it would be wise to find items in the chain. I understand that I see 3 things to look for 1) Mathematical symbols: (+*/-) 2) Figures 3) The strings (short, everything except the above). What I translate into this: Code: Final String symbols = "[+-/*()]"; / / A character from the following: + - * / () Final String numbers = "\\d + (\\.\\d *)? "; / / Numbers: at least one number, possibly followed by '. " and optional data Final String string = "[^+-/*()\\d] + "; / / A string: All who are no numbers or symbols Code: String input = "(Other receivables + Accounts receivable) / 5.25 * ARRESTED IN NV + Shipping preliminary";
Pattern pattern = Pattern.compiles("("+ + symbols")|("numbers + +")|("+ + strings")");
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
System.out.System.out.println("Found: [" + Matcher.group() + "]");
} |
|
#6
| |||
| |||
| Re: Extract the elements of string in java
Hello, I implemented you code and here is the result Code: Final String symbols = "[+-/*()]"; / / A character from the following: + - * / ()
Final String numbers = "\\d + (\\.\\d *)? "; / / Numbers: at least one number, possibly followed by '. " and optional data
Final String string = "[^+-/*()\\d] + "; / / A string: All who are no numbers or symbols
String input = "(Other receivables + Accounts receivable) / 5.25 * ARRESTED IN NV + Shipping preliminary";
Pattern pattern = Pattern.compiles("("+ + symbols")|("numbers + +")|("+ + strings")");
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
System.out.System.out.println("Found: [" + Matcher.group() + "]");
} |
![]() |
|
| Thread Tools | Search this Thread |
| |
Similar Threads for: "Extract the elements of string in java" | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Sort the elements of a table in java | Adrina_g | Software Development | 4 | 06-03-2010 11:59 AM |
| Extract the integer from a string | New ID | Software Development | 5 | 26-02-2010 12:31 AM |
| Java program to reverse the order of LinkedList elements? | MKAIF | Software Development | 5 | 22-01-2010 09:44 PM |
| How to sort LinkedList elements using java program? | KADRI | Software Development | 4 | 22-01-2010 08:45 PM |
| Extract Java File From Sony Ericsson K850 | Level8 | Portable Devices | 5 | 21-12-2009 02:47 PM |