Regular Expression problem

Discussion in 'Java' started by dhinakar_ve@yahoo.com, Aug 10, 2007.

  1. Guest

    I am trying to parse the string algoParameters to get the strings
    between the delimiter | using java.util.regex.Pattern class. But I am
    not able to get the characters after the letter "-". What is the
    metacharacter to represent any ascii character other than whitespaces.
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    String algoParameters = "57=PSHORT|5062=20050110-15:01:00|
    5063=20050110-16:01:00|";

    Pattern algoPattern = Pattern.compile("\\d+=[\\w]+
    [|]?");
    Matcher m = algoPattern.matcher(algoParameters);
    while (m.find())
    {
    String matchedText = m.group();
    int matchedFrom = m.start();
    int matchedTo = m.end();
    System.out.println("matched [" + matchedText +
    "] " + "from " + matchedFrom + " to " + matchedTo + ".");

    System.out.println(algoParameters.substring(matchedFrom, matchedTo));
    }
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    But I get the following output:
    matched [57=PSHORT|] from 0 to 10.
    57=PSHORT|
    matched [5062=20050110] from 10 to 23.
    5062=20050110
    matched [5063=20050110] from 33 to 46.
    5063=20050110

    I am expecting
    57=PSHORT
    5062=20050110-15:01:00
    5063=20050110-16:01:00
     
    , Aug 10, 2007
    #1
    1. Advertising

  2. wrote:
    > I am trying to parse the string algoParameters to get the strings
    > between the delimiter | using java.util.regex.Pattern class. But I am
    > not able to get the characters after the letter "-". What is the
    > metacharacter to represent any ascii character other than whitespaces.
    > //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    > String algoParameters = "57=PSHORT|5062=20050110-15:01:00|
    > 5063=20050110-16:01:00|";
    >
    > Pattern algoPattern = Pattern.compile("\\d+=[\\w]+
    > [|]?");
    > Matcher m = algoPattern.matcher(algoParameters);
    > while (m.find())
    > {
    > String matchedText = m.group();
    > int matchedFrom = m.start();
    > int matchedTo = m.end();
    > System.out.println("matched [" + matchedText +
    > "] " + "from " + matchedFrom + " to " + matchedTo + ".");
    >
    > System.out.println(algoParameters.substring(matchedFrom, matchedTo));
    > }
    > ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    > But I get the following output:
    > matched [57=PSHORT|] from 0 to 10.
    > 57=PSHORT|
    > matched [5062=20050110] from 10 to 23.
    > 5062=20050110
    > matched [5063=20050110] from 33 to 46.
    > 5063=20050110
    >
    > I am expecting
    > 57=PSHORT
    > 5062=20050110-15:01:00
    > 5063=20050110-16:01:00


    You could explicit allow minus and colon as characters.

    Non whitespace is \S, but that would match | as well.

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Aug 10, 2007
    #2
    1. Advertising

  3. Roedy Green Guest

    Roedy Green, Aug 10, 2007
    #3
  4. Guest

    On Aug 9, 10:14 pm, Roedy Green <>
    wrote:
    > > What is the
    > >metacharacter to represent any ascii character other than whitespaces.

    >
    > seehttp://mindprod.com/jgloss/regex.html
    > \S \S+
    > related (\p{Alpha}+)
    > --
    > Roedy Green Canadian Mind Products
    > The Java Glossaryhttp://mindprod.com


    \S+ matches the whole line. I get the original string as the match.
     
    , Aug 10, 2007
    #4
  5. Michael Jung Guest

    writes:
    > On Aug 9, 10:14 pm, Roedy Green <>
    > wrote:
    > > > What is the
    > > >metacharacter to represent any ascii character other than whitespaces.

    > >
    > > seehttp://mindprod.com/jgloss/regex.html
    > > \S \S+
    > > related (\p{Alpha}+)
    > > --
    > > Roedy Green Canadian Mind Products
    > > The Java Glossaryhttp://mindprod.com

    > \S+ matches the whole line. I get the original string as the match.


    Split the beast on | first. Or do \d=[^|]+

    Michael
     
    Michael Jung, Aug 10, 2007
    #5
  6. dhina Guest

    On Aug 10, 2:39 am, Michael Jung <> wrote:
    > writes:
    > > On Aug 9, 10:14 pm, Roedy Green <>
    > > wrote:
    > > > > What is the
    > > > >metacharacter to represent any ascii character other than whitespaces.

    >
    > > > seehttp://mindprod.com/jgloss/regex.html
    > > > \S \S+
    > > > related (\p{Alpha}+)
    > > > --
    > > > Roedy Green Canadian Mind Products
    > > > The Java Glossaryhttp://mindprod.com

    > > \S+ matches the whole line. I get the original string as the match.

    >
    > Split the beast on | first. Or do \d=[^|]+
    >
    > Michael


    \d=[^|]+ works.
    Thanks for the help.
    Dhina
     
    dhina, Aug 10, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. godfather2

    Regular Expression problem

    godfather2, Jul 20, 2003, in forum: Perl
    Replies:
    1
    Views:
    1,006
    Shawn Corey
    Jul 20, 2003
  2. VSK
    Replies:
    2
    Views:
    2,334
  3. Nazir
    Replies:
    3
    Views:
    5,084
    jayendra gadhavi
    Jan 2, 2008
  4. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    861
    Alan Moore
    Dec 2, 2005
  5. GIMME
    Replies:
    3
    Views:
    11,997
    vforvikash
    Dec 29, 2008
Loading...

Share This Page