Results 1 to 6 of 6
  1. #1
    orkun is offline Member
    Join Date
    Feb 2009
    Posts
    6
    Rep Power
    0

    Default problem with using regular expression

    hello

    I have huge strings and from which I want to extract this form: [ *,* ] using regular expression.

    here is the fragment of the string.

    { "type": "Polygon", "coordinates": [ [ [ 32.090000, 41.110000 ], [ 32.090537, 41.110510 ], [ 32.091100, 41.110991 ], [ 32.100000, 41.114142 ], [ 32.100740, 41.114123 ], [ 32.101478, 41.114065 ] ] ]}

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Try the following

    ".*\\[(.*\\,.*)\\].*"

    Then you can use the group(int group) method of the Matcher class to access the matching capture groups.


    ...if you want the result to include the square brackets, then move those inside the parentheses.

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    or better: use a json parser like gson or jackson :D

  4. #4
    orkun is offline Member
    Join Date
    Feb 2009
    Posts
    6
    Rep Power
    0

    Default

    thank you

    I am actually JSON format.
    So how can I use JSON and can you recommend any reference material ?

    regards

  5. #5
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    JSON
    and gson guide Gson User Guide - gson
    +
    jackson tutorial: Jackson JSON Processor - Tutorial

    are you sure, that you want an array in an array in an array ? :D ("coordinates": [ [ [ ) ?
    thats strange imo :D

    here is an little gson example with your json string

    simple java class
    Java Code:
    class Shape {
    	private String type;
    	private Double[][][] coordinates; // or private List<Double[][]> coordinates; or something similar
    
    	/**
    	 * @return the type
    	 */
    	public String getType() {
    		return type;
    	}
    
    	/**
    	 * @return the coordinates
    	 */
    	public Double[][][] getCoordinates() {
    		return coordinates;
    	}
    }
    parse
    Java Code:
    	public static void main(String[] args) {
    		String json = "{ \"type\": \"Polygon\", \"coordinates\": [ [ [ 32.090000, 41.110000 ], [ 32.090537, 41.110510 ], [ 32.091100, 41.110991 ], [ 32.100000, 41.114142 ], [ 32.100740, 41.114123 ], [ 32.101478, 41.114065 ] ] ]} ";
    		Shape s = new Gson().fromJson(json, Shape.class);
    		System.out.println(s.getType());
    		for (Double[][] arr : s.getCoordinates()) {
    			for (Double[] doubles : arr) {
    				System.out.println(Arrays.toString(doubles));
    			}
    		}
    	}

  6. #6
    orkun is offline Member
    Join Date
    Feb 2009
    Posts
    6
    Rep Power
    0

    Default

    thank you

    I will study on the example you sent to me.

    the reason I sent the initial mail was to eliminate all words and [[[ (replace it to [[)

    So I aim to get [ [ 32.033333, 41.166667 ], [ 32.036914, 41.170064 ], ....] for javascript array.

Similar Threads

  1. Help with regular expression
    By mr.ab18 in forum New To Java
    Replies: 2
    Last Post: 08-06-2010, 10:01 PM
  2. regular expression
    By prof.deedee in forum JDBC
    Replies: 3
    Last Post: 02-19-2010, 11:15 AM
  3. regular expression
    By QkrspCmptPop in forum Advanced Java
    Replies: 8
    Last Post: 01-20-2010, 03:55 AM
  4. regular expression
    By ras_pari in forum Advanced Java
    Replies: 27
    Last Post: 10-07-2009, 12:25 PM
  5. Regular Expression Problem
    By daflores in forum Advanced Java
    Replies: 8
    Last Post: 02-10-2009, 06:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •