Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Regular Expression problem

Reply
Thread Tools

Regular Expression problem

 
 
dhinakar_ve@yahoo.com
Guest
Posts: n/a
 
      08-10-2007
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(matche dFrom, 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

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      08-10-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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(matche dFrom, 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
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      08-10-2007
> What is the
>metacharacter to represent any ascii character other than whitespaces.


see http://mindprod.com/jgloss/regex.html
\S \S+
related (\p{Alpha}+)
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
dhinakar_ve@yahoo.com
Guest
Posts: n/a
 
      08-10-2007
On Aug 9, 10:14 pm, Roedy Green <(E-Mail Removed)>
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.

 
Reply With Quote
 
Michael Jung
Guest
Posts: n/a
 
      08-10-2007
(E-Mail Removed) writes:
> On Aug 9, 10:14 pm, Roedy Green <(E-Mail Removed)>
> 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
 
Reply With Quote
 
dhina
Guest
Posts: n/a
 
      08-10-2007
On Aug 10, 2:39 am, Michael Jung <(E-Mail Removed)> wrote:
> (E-Mail Removed) writes:
> > On Aug 9, 10:14 pm, Roedy Green <(E-Mail Removed)>
> > 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Seek xpath expression where an attribute name is a regular expression GIMME XML 3 12-29-2008 03:11 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
Matching abitrary expression in a regular expression =?iso-8859-1?B?bW9vcJk=?= Java 8 12-02-2005 12:51 AM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments