Results 1 to 6 of 6

Thread: Extract the elements of string in java

  1. #1
    Join Date
    Dec 2009
    Posts
    204

    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.
    (Other receivables + Accounts receivable)/5.25* ARRESTED IN NV + P Preliminary
    the desired result after insertion into the vector cad vector content:
    will be as follows
    Vector v =[(, Other debtors + Accounts receivable,),/,5.25, *, STANDING IN NV, +, preliminary expenses]
    It has a function to do this treatment.
    Last edited by Ash maker; 13-01-2010 at 01:52 PM.

  2. #2
    Join Date
    May 2008
    Posts
    2,389

    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.

  3. #3
    Join Date
    Feb 2008
    Posts
    1,852

    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("\\+|\\*|/");
    I think this is the correct choice for your situation.

  4. #4
    Join Date
    Dec 2009
    Posts
    204

    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?
    String s ="(Other receivables + Accounts receivable) / 5.25 * ARRESTED IN NV + Shipping preliminary";
    String[] res = s.split("\\+|\\*|/");
    System.out.print(res);
    the result is the following:
    [(, Other Receivables, Accounts receivable,),5.25, STANDING IN NV, preliminary expenses]
    It Does not operators +,*,/ what is obvious, but my objective is to insert the same operators +,*,/ but respecting their orders in the chain.

  5. #5
    Join Date
    Apr 2008
    Posts
    2,005

    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
    Then just look for these items:
    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() + "]");
    	}
    Now if your need is more complex it will probably define a grammar and use a parser generator such as Javac.

  6. #6
    Join Date
    Dec 2009
    Posts
    204

    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() + "]");
    	}

Similar Threads

  1. Sort the elements of a table in java
    By Adrina_g in forum Software Development
    Replies: 4
    Last Post: 06-03-2010, 11:59 AM
  2. Extract the integer from a string
    By New ID in forum Software Development
    Replies: 5
    Last Post: 26-02-2010, 12:31 AM
  3. Java program to reverse the order of LinkedList elements?
    By MKAIF in forum Software Development
    Replies: 5
    Last Post: 22-01-2010, 09:44 PM
  4. How to sort LinkedList elements using java program?
    By KADRI in forum Software Development
    Replies: 4
    Last Post: 22-01-2010, 08:45 PM
  5. Extract Java File From Sony Ericsson K850
    By Level8 in forum Portable Devices
    Replies: 5
    Last Post: 21-12-2009, 02:47 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,718,343,359.02787 seconds with 17 queries