Parsing a string using StringTokenizer

Hi,
I want to parse a string such as
String input = ab{cd}:"abc""de"{
and the extracted tokens should be as follows
ab
cd
"abc""de"
As a result, I used the StringTokenizer class with deilmeter {,},:
StringTokenizer tokenizer = new StringTokenizer(input,"{}:", true);
In this was, I can separate the tokens and also can get the delimeters. The problem is I don't know how to parse the string that has double quote on it. If a single quote " is taken as a delimeter then
", abc, ",", de," all of them will be taken as a separate token. My intention is to get the whole string inside the double quote as a token including the quotes on it. Moreover, if there is any escape character "", it should be also included in the token. Help please.
Thanks

A bit of a "sticky tape"-solution...
import java.util.StringTokenizer;
public class Test {
    public static void main(String[] args) {
        String input = "ab{cd}:\"abc\"\"de\"";
        StringTokenizer st = new StringTokenizer(input, "{}:", true);
        while(st.hasMoreTokens()) {
        String token = st.nextToken();
        if(token.startsWith("\"") && token.endsWith("\"")) {
        token = token.substring(1,token.length()-1);
            System.out.println(token);
}

Similar Messages

  • Need Help Parsing JSON String using PLJSON

    Hello,
    I have JSON stored in a database field:
    --Query
    select data1 from table1 where id= 339207152427;
    --Results:
    [{"name":"Home"},{"code":"JPNWC74ZKW9"},{"start date":"01\/02\/2014"},{"person name":"hhh, RamS"}]
    I need to parse the above results into 3 fields in a separate table. For now, i'm just trying to parse the results using PLJSON, but am getting the ORA-20101: JSON Parser exception - no { start found error when running the following pl/sql block:
    declare
    VIN_JSON varchar2(4000);
    jsonObj json;
    listOfValues json_list;
    listElement json_value;
    begin
    select data1 into VIN_JSON from table1 where id= 339207152427;
    jsonObj := new json(VIN_JSON);
    listOfValues := jsonObj.get_values();
    for i in 1..listOfValues.count loop
    listElement := listOfValues.get(i);
    end loop;
    end;
    I believe my syntax is close, but was hoping for some further instruction.
    Thanks in advance!
    John

    I have no idea what you are trying to do or in what version so help, at this point, is not possible.
    Let's start with this:
    I need to parse the above results into 3 fields in a separate tableAnd the parsing rules?
    And what the result should be?
    You apparently want us to guess. Which is something most of us do not like to do. (and don't forget your full version number if you want help).
    PS: This forum has a FAQ: I recommend you read it as you didn't even put your listing into tags.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • How to parse xml string using JSTL

    Suppose this is my string:-----
    <books>
    <book>
    <title id=1>Book Title A</title>
    <author>A. B. C.</author>
    <price>17.95</price>
    </book>
    <book>
    <title id=2>Book Title B</title>
    <author>X. Y. Z.</author>
    <price>24.99</price>
    </book>
    </books>
    and I want to read title id = 1 then, how to parse it, I found tutorials regarding parsing simple xml document but how to parse attributes of a given XML

    But both of them either parse a file or data from an input source. I don't think they handle strings.An InputSource can be constructed with a Reader as input. One useful subclass of Reader is StringReader, so you'd use something likeDocument doc = documentBuilder.parse(new InputSource(new StringReader(myXMLString)));

  • How to disply empty cell from a string using stringtokenizer class

    Hello All
    I had wriiten a small program to read the string and split the string into different tuples.When i had empty cell in string i am unable to display it. Can any body help me out pls?
    code given below
    import java.io.*;
    import java.util.StringTokenizer;
    class Tokenize {
    // Create BufferedReader class instance
    static InputStreamReader input = new InputStreamReader(System.in);
    static BufferedReader keyboardInput = new BufferedReader(input);
    static String record;
    /* Main method */
    public static void main(String[] args) throws IOException {
         //declare the string variables
         String text,delim,var,delimv,vname;
         String X = "x";
         int i = 1;
         //read the inputs from keyboard for variables and delimv
         System.out.print("What is your variable names? ");
    var = keyboardInput.readLine();
    System.out.println("variable are :" + var );
         System.out.print("What is your separator? ");
         delimv = keyboardInput.readLine();
         //read the inputs from keyboard for string and delim
    System.out.print("What is your String? ");
    text = keyboardInput.readLine();
    System.out.println("String is :" + text );
         System.out.print("What is your separator? ");
         delim = keyboardInput.readLine();
         //create the stringtokenizer class instance
         StringTokenizer vt = new StringTokenizer(var,delimv);
         StringTokenizer st = new StringTokenizer(text,delim);
         //print the nexttokens as long as tokens and variables are available
         while (vt.hasMoreTokens()) {
         while (st.hasMoreTokens()) {
         vname = vt.nextToken();
         //     if(st.nextElement() == X){
         // System.out.println("filed is empty");
         // System.out.println(vname + "= ");
         System.out.println(vname + "=" + st.nextToken());
                   //nextToken());
    Variables name : cat1,cat2,cat3,cat4,cat5,cat6,cat7,cat8,cat9,cat10,cat11
    String name : Produkten;Voor hem;;Funny Mask ;12.50;4.90;17.40;1-5 dagen;0;http://www.eroticastore.nl/ProductDetails.asp?ProductId=21733&ReferrerId=134;http://www.eroticastore.nl/ProductImages/thumb_3300000510.jpg
    Note : Empty cell is in between "Voor hem" and "Funny Mask"
    Thanks In advance,
    Murali

    When you post code, please use [code] and [/code] tags as described in Formatting Help on the message entry page. It makes it much easier to read.

  • Parse SQL string using Zql or SQL4J

    any used zql or sql4j.
    if then plz give me some example

    import org.apache.xerces.dom.DOMParser;
    import org.w3c.dom.Document;
    import org.w3c.dm.NodeList;
    DOMParser parser new DOMParser();
    parser.parse("xyz.xml"); // Instead of xml file it could be a InputStream.. not sure though
    // You need to llok at documentation
    Document doc = parser.getDocument()
    // Once you have got the Document you can manipulate it using DOM api
    // For example following code prepares a list of nodes having a tag = "product_id"
    NodeList prodList = new NodeList();
    prodList = doc.getNodeByTagName("product_id");
    Hope this helps !!!
    Sunil

  • Parsing Query string using Client Object model [ECMA]

    hello
    i want to access querystring using client object model using ECMA script.
    for eg http://localhost:80/demopage.aspx?test=123
    can we read value of test using ECMA script
    regards
    Manish

    thanks ...
    i did that...using
    <script type="text/javascript">
    function queryString(parameter) {
    var loc = location.search.substring(1, location.search.length);
    var param_value = false;
    var params = loc.split("&");
    for (i=0; i<params.length;i++) {
    param_name = params[i].substring(0,params[i].indexOf('='));
    if (param_name == parameter) {
    param_value = params[i].substring(params[i].indexOf('=')+1)
    if (param_value) {
    return param_value;
    else {
    return false; //Here determine return if no parameter is found

  • Problem in parsing a xml string using dom parser

    i want to parse a Xml String using a Dom parser......the parse function in dom parser takes only input stream as argument.......so i made the code as
    InputStream inputstream = new StringBufferInputStream(XmlData) ;
    InputSource inputSource = new InputSource(inputstream );
    but saxexception is coming and also warning called
    "java.io.StringBufferInputStream in java.io has been deprecated"
    please help me.........

    i want to parse a Xml String using a Dom
    parser......the parse function in dom parser takes
    only input stream as argument.......This is not true of the DOM parser in Java 1.4. So you might want to get rid of your old parser and replace it by something more current. Or perhaps you are using 1.4 and you just didn't read all of the API docs.

  • Parse a string that uses nested string delimiters

    I’m looking for a way to parse a string that uses double quotes as the delimiter that contains a nested string with double quotes.  I want to keep the double quotes in the nested string.  Is there a function in Labview that I can use to easily do this?  Does Labview have a command line parser / interpreter?
    The string example is: While match "Send Test "Sleep 5" ">""
    The result string would be:  Send Test "Sleep 5" ">"

    Here is another solution which is slightly faster.  Speed won't be
    an issue unless you are running millions at a time, as in my test the
    difference was only 1-2 ms in 10000 iterations.  In this image the
    Match Pattern function retrieves the desired string, including the
    outside quotes, so the String Length and String Subset functions are
    used to remove the first and last characters of the result.
    Message Edited by tuba on 04-11-2006 10:35 PM
    Attachments:
    parse.gif ‏3 KB

  • Parse for multiple occurences of something in query string used

    HI
    A varchar is passed as an input to a stored proc. This could have zero, one or more occurances of a word (e.g. from_no). This will be in form of a name value pair and could occur multiple times. Exampe
    query_string = ' abc=123 and from_no > 1 and from_no < 10 and def = 1'
    Now I need to parse this string to get the operator and values for the 2 occurences of from_no. How can I do that? How can I go about writing a smple func or sp to parse and return both from_no's?

    Perhaps this sample code can help:
    with yourtable as
    (select ' abc=123 and from_no > 1 and from_no < 10 and def = 1' query_string from dual union all
    select ' abc=123 and from_no = 2 and from_no != 30 and def = 1 and from_no=40' query_string from dual)
    select  query_string,
            x.n no,
            regexp_substr(query_string,'from_no *[<>!=]* *[0-9]*',1,x.n) exp,       
            regexp_replace(regexp_substr(query_string,'from_no *[<!>=]* *[0-9]*',1,x.n),'.*from_no *([<!>=]*) *([0-9]*).*','\1') op,
            regexp_replace(regexp_substr(query_string,'from_no *[<!>=]* *[0-9]*',1,x.n),'.*from_no *([<!>=]*) *([0-9]*).*','\2') val
    from yourtable,
         (select rownum n from dual connect by rownum<=100) x
    where regexp_instr(query_string,'from_no',1,x.n)>0   --  end searching in query string when there are no more expressions
    order by query_string, x.nI added one more example.
    It works with your example, but you will have to adapt the code for your real environment.
    This code supports only the operators: =, !=, <, >
    This code supports only numeric values without decimal-point.
    Edited by: hm on 19.10.2010 22:46

  • Parsing and String tokenizer

    I am trying to use String Tokenizer to extract fields from a file then do a calculation on some of the fields.
    My problem is when I try to parse the strings that need the calculations I get error codes as shown at the end of post. I don't see why the parsing shouldn't work.( error codes are class expected and incompatable types)
    // ParsingTextFile.java: Process text file using StringTokenizer
    import java.io.*;
    import java.util.StringTokenizer;
    public class ParsingTextFile1 {
      /** Main method */
      public static void main(String[] args) {
       // Declare  buffered file reader and writer streams
       BufferedReader brs = null;
       BufferedWriter bws = null;
       //declare tokenizer
       StringTokenizer tokenizer;
       // Declare a print stream
       PrintWriter out = null;
        // Five input file fields:total string, student name, midterm1,
        // midterm2, and final exam score
        String line=null;
        String sname = null;
       String mid1= null;
        String mid2 = null;
        String finalSc = null;
      /  double midterm1 = 0;
        double midterm2 = 0;
        double finalScore = 0;
        // Computed total score
        double total = 0;
        try {
          // Create file input and output streams 
          brs = new BufferedReader(new FileReader("in.dat"));
          bws = new BufferedWriter(new FileWriter("out.dat"));
         while((line= brs.readLine())!=null){
         tokenizer =new StringTokenizer(line);
          sname = tokenizer.nextToken();
          mid1 = tokenizer.nextToken();
          mid2 = tokenizer.nextToken();
          finalSc= tokenizer.nextToken();
          midterm1 = double.parseDouble(mid1)//this code not working
          midterm2 = double.parseDouble(mid2);)//this code not working
          finalScore = double.parseDouble(finalScore);//this code not working
          out = new PrintWriter(bws);
          total = midterm1*0.3 + midterm2*0.3 + finalScore*0.4;
          out.println(sname + " " + total);
        catch (FileNotFoundException ex) {
          System.out.println("File not found: in.dat");
        catch (IOException ex) {
          System.out.println(ex.getMessage());
        finally {
          try {
            if (brs != null) brs.close();
            if (bws != null) bws.close();
          catch (IOException ex) {
            System.out.println(ex);
    }[\code]
    errorsC:\j2sdk1.4.1_06\bin\ParsingTextFile1.java:43: class expected
        midterm1 = double.parseDouble(mid1);
                          ^
    C:\j2sdk1.4.1_06\bin\ParsingTextFile1.java:44: class expected
        midterm2 = double.parseDouble(mid2);
                          ^
    C:\j2sdk1.4.1_06\bin\ParsingTextFile1.java:45: class expected
        finalScore = double.parseDouble(finalScore);
                            ^
    C:\j2sdk1.4.1_06\bin\ParsingTextFile1.java:43: incompatible types
    found   : java.lang.Class
    required: double
        midterm1 = double.parseDouble(mid1);
                         ^
    C:\j2sdk1.4.1_06\bin\ParsingTextFile1.java:44: incompatible types
    found   : java.lang.Class
    required: double
        midterm2 = double.parseDouble(mid2);
                         ^
    C:\j2sdk1.4.1_06\bin\ParsingTextFile1.java:45: incompatible types
    found   : java.lang.Class
    required: double
        finalScore = double.parseDouble(finalScore);
                           ^
    6 errors
    Process completed.

    while(line.hasMoreTokens())
    sname = tokenizer.nextToken();  
       mid1 = tokenizer.nextToken();   
      mid2 = tokenizer.nextToken();  
       finalSc= tokenizer.nextToken(); 
        midterm1 = double.parseDouble(mid1)//this code not working
         midterm2 = double.parseDouble(mid2);)//this code not working  
       finalScore = double.parseDouble(finalScore);//this code not working
    }if it doesn't work then you need to add delimiter Or the token that you are trying to convert into is not valid.

  • Anyone know how I can parse this string?

    Hello everyone!
    I'm using the Scanner class (which is like the StringTokenizer class but suppose to be more powerful).
    Anyways, I have the following String:
    OP '1.3.12.2.1004.212'.'1.3.12.2.1004.195'
    I want to parse the String so I have the following:
    '1.3.12.2.1004.212'
    '1.3.12.2.1004.195'
    so basically just parse it by the middle dot, I tried useDelimeter function and it doesn't parse it up at all. You would think it would have parsed every occurance of . but that isn't the case.
    Here is my code:
    else if(line.contains("OP"))
                             System.out.println("HIT THE SUBFIELD PART lulz");
                             String subClassName = "";
                             String subFieldName = "";
                             scan = new Scanner(line);
                             scan.useDelimiter(".");
                             System.out.println("LINE: " + line);
                             while(scan.hasNext())
                                  System.out.println("Token: " + scan.next());
                             }The output is the following:
    HIT THE SUBFIELD PART LOL
    LINE: OP '1.3.12.2.1004.212'.'iPAddress'
    Token:
    Token:
    Token:
    Token:
    Token:
    Token:
    Token:
    Any ideas what I can do to parse it up the way I mentioned above? thanks!

    hm..it just occured to me I could do the following
    else if(line.contains("OP"))
                             System.out.println("HIT THE SUBFIELD PART lulz");
                             String subClassName = "";
                             String subFieldName = "";
                             scan = new Scanner(line);
                             scan.skip("OP");
                             scan.useDelimiter("'.'");
                             System.out.println("LINE: " + line);
                             while(scan.hasNext())
                                  System.out.println("LOOK HERE: " + scan.next());
                             }I get the following output:
    LINE: OP '1.3.12.2.1004.212'.'iPAddress'
    LOOK HERE: '1.3.12.2.1004.212
    LOOK HERE: iPAddress'
    So I guess I can just manually manipulate that last token and readd the ' character!
    I think thats what I will do but if you can find a bettter solution let me know! thanks! :D

  • Spliting a large string using regular expression which contain special char

    I have huge sting(xml) containing normal character a-z,A-Z and 0-9 as well as special char( <,>,?,&,',",;,/ etc.)
    I need to split this sting where it ends with </document>
    for e.g.
    Original String:
    <document>
    <item>sdf</item>
    <item><text>sd</text</item>
    </document>
    <document>hi</document>
    The above sting has to be splited in to two parts since it is having two document tag.
    Can any body help me to resolve this issue. I can use StringTokenizer,String split method or Regular expression api too.

    manas589 wrote:
    I used DOM and sax parser and got few exception. Again i don't have right to change xml. so i thought to go with RegularExpression or some other way where i can do my job.If the file actually comes in lines like what you posted, you should just be able to compare the contents of each line to see if it contains "</document>" or whatever you're looking for. I wouldn't use regex unless I needed another problem.
    I got excpetion like: Caused by: org.xml.sax.SAXParseException: The entity "nbsp" was referenced, but not declared.
         at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
         at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source)
         at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)So then it isn't even XML.
    Edit: sorry, I just realized why you're considering all of these heavy-duty ideas. It's just that you don't know how to break the string into lines. You do it like this:
    BufferedReader  br = new BufferedReader(new StringReader(theNotXMLString));

  • Can i still use StringTokenizer?

    I am doing a an organizer/planner project that reads from a text data file.
    Each line of the text file is an appointment in the format:
    YYYY-MM-DD HH:MM HH:MM Appointment
    The first time is Start time and second one is the duration.
    Here is the code i initially have, where dis is of class DataInputStream:
    appointment = dis.readLine();
    StringTokenizer st = new StringTokenizer(appointment, "-:\t "); //skip
    tab, space, - and :
    String year_string = st.nextToken();
    String month_string = st.nextToken();
    String day_string = st.nextToken();
    String app_hour_string = st.nextToken();
    String app_min_string = st.nextToken();
    String dur_hour_string = st.nextToken(); // problem in here, now it can
    be just one token as well
    String dur_min_string = st.nextToken();
    String desc = st.nextToken("").trim();
    There is a slight change of spec and now the duration can be in the form of
    either hh:mm or just mm. Here the problem arise,
    since the appiontment field can have spaces/whitespace in it, so i do
    "String desc = st.nextToken("").trim();" to over come that,
    but in doing so, i can't tell where i am in the line, since i treat
    everything in the end as the appointment, but now the duration can
    be one or two token, can somebody suggest a possible remedy? TIA
    Regards,
    Ben

    What I would be doing is reading "YYYY-MM-DD HH:MM" as two tokens (split at the spaces), stick them together and use a DateFormat to parse the string into a Date object.
    Then I would read the next token (HH:MM or MM), and use two different DateFormat objects to see which one will parse the Date successfully. With enough luck, the Date object will keep all the other fields at zero, and just initialise the hours or minutes, as appropriate.

  • Getting Error while creating Document object  after  parsing XML String

    Hi All,
    I have been trying to parse an XML string using the StringReader and InputSource interface but when I am trying to create Document Object using Parse() method getting error like
    [Fatal Error] :2:6: The processing instruction target matching "[xX][mM][lL]" is not allowed.
    seorg.xml.sax.SAXParseException: The processing instruction target matching "[xX][mM][lL]" is not allowed.
    Please find the code below which i have been experimenting with:
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.StringReader;
    import java.util.List;
    import java.util.*;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.InputSource;
    import org.xml.sax.SAXException;
    import java.io.*;
    public class TestMain {
         public static void main(String[] args) {
              String file = "";
              file = loadFileContent("C:\\Test.xml");
              System.out.println("contents >> "+file);
              parseQuickLinksFileContent(file);
    public static void parseQuickLinksFileContent(String fileContents) {
    PWMQuickLinksModelVO objPWMQuickLinksModelVO = new PWMQuickLinksModelVO();
         try {
    DocumentBuilderFactory factory =           DocumentBuilderFactory.newInstance();
         DocumentBuilder builder = factory.newDocumentBuilder();
         StringReader objRd = new StringReader(fileContents);
         InputSource objIs = new InputSource(objRd);
         Document document = builder.parse(objIs); // HERE I am getting Error.
         System.out.println(document.toString());
    What is happening while I am using builder.parse() method ???
    Thanks,
    Rajendra.

    Getting following error
    [Fatal Error] :2:6: The processing instruction target matching "[xX][mM][lL]" is not allowed.
    seorg.xml.sax.SAXParseException: The processing instruction target matching "[xX][mM][lL]" is not allowed.

  • Parsing formatted String to Int

    How can I parse formatted string to Integer ?
    I have a formated string like this $900,000 and I need to convert it to 900000 so I could do calculations with it.
    I tried something like this
    NumberFormat nf = NumberFormat.getIntegerInstance(request.getLocale());
    ttlMargin=nf.parse(screenVal);I got this exception
    "java.lang.NumberFormatException: For input string: "$1,050,000""

    I am working on the JSP file that provides
    margins,sales etc. I am reading this data off the
    screen where it is beeing displayed according to the
    accounting practices.
    That's why I get it as a formatted string and why I
    am trying covert that string to the numberScreen-scraping is a problematic, bad design. It sounds like what you really want is to call a web service which returns its results as data that a program can understand (XML, for example), not HTML (which is meant more for humans to read). I know, you probably can't change the design at this point... just food for thought. In the meantime, you'll probably have to manually parse those strings yourself by stripping out the '$' and ',' characters and then use parseInt on the result.

Maybe you are looking for

  • XML Publisher template. Can't associate to a report

    Hi, I made a report with xml output. With it's output I define an rtf template and associate it to an XML Publisher template. When I execute this report, I try to choose the template to format the output to excel (in Options button), but lov is empty

  • Load XML file into either List MyType or ObservableCollection MyType

    I have been searching and trying different suggestions for three days now, full time.  I am building a universal app for Windows 8.1 and Windows Phone 8.1.  The problem, apparently, is that doing this is different between Windows 7.1, Windows Phone 7

  • Error starting Oracle Application server

    Hi I installed Oracle application server 10g. I successfully installed it. But when i stopped the opmn and emctl process and now when i am trying to start i am able to successfully start but when i type the URL of OAS Admin console. I get following e

  • How to do DUMP Analysis ?

    How to do DUMP Analysis ?Can anybody explain me the steps to solve the dump step by step ? thanks Naresh

  • Combining files

    First, I am trying to merge 2 pdf files (one is 6pages and the other is 2 pages} into one linkthis one file to my website. Also I would like to use only 1 page from the 6 page file and both of the 2 page file. I have the free version of Acrobat with