• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

simple regex

 
kevind duff
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All at the Ranch:

I hope some will be kind enough to point out my error.

The below code produces the message below it.
When this message or a similar message is returned I want to trap it.
The regex expression work in the Regulator, but it does not work
in the matches below.
if ( strBuffer.toString().matches("LX_CODE.{0,10}LX_MESSAGE") )


Any hint as to what my error is? please

thanks

KBD






LX_CODE: 8 LX_MESSAGE: Data Format of xcf not supported, use XML or CSV only
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you read the docs for String.matches(), you'll see it tries to match the entire input. The regex you have will match a String that starts with "LX_CODE", has 0 to 10 of any character, then has "LX_MESSAGE" and then ends. If you need to match "LX_CODE: 8 LX_MESSAGE: Data Format of xcf not supported, use XML or CSV only" then try adding ".*" at the end of your regex.
 
kevind duff
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


thanks that worked perfectly.
I forgot to Read the Fine Manual (RTFM)

thanks aging

KD
 
kevind duff
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Again regex baffles me.

I am retrieving content from a web site as a text string.
Beginning of the text retrieved looks like this:

LoanX ID,Mark Date,Bid,Offer,Depth,Evaluated Price,Close Bid,Close Offer,Close Date,Contributed
"LX015353","2001-07-05",0.5,1.5,"1",1,0.5,1.5,"2012-10-05","N"
"LX013663","2001-07-05",80.125,90.125,"1",85.125,80.125,90.125,"2012-10-05","N"
"LX015664","2001-07-05",0.125,0.375,"1",0.25,0.125,0.375,"2012-10-05","N"
"LX01QQ76","2001-07-05",92,94,"1",93,92,94,"2012-10-05","N"
"LX01QA14","2001-07-05",92,94,"1",93,92,94,"2012-10-05","N"

Using the code below, this works to catch the error condition
if ( strBuffer.toString().matches("LX_CODE.{0,10}LX_MESSAGE.*") ) {

but this code
else if ( strBuffer.toString().matches( "LoanX.*" ) ) {
does not catch when valid results are returned.

Why?

I have tried a number of variations on "LoanX.*", like "^LoanX.*", etc...

What I want to do is match the full first line.
I thought this regex would do it:
"^LoanX\\sID,Mark\\sDate,Bid,Offer,Depth,Evaluated\\sPrice,Close\\sBid,Close\\sOffer,Close\\sDate,Contributed.*"
but no luck.

your kind assistance is requested.

thanks

KD





System.out.println (strBuffer.toString());
if ( strBuffer.toString().matches("LX_CODE.{0,10}LX_MESSAGE.*") ) {
logger.error("Error Message Returned from MarkIt --> will shutdown now");
logger.error(strBuffer.toString());
strBuffer = null;
fLoop = -9;

} // "^LoanX\\sID,Mark\\sDate,Bid,Offer,Depth,Evaluated\\sPrice,Close\\sBid,Close\\sOffer,Close\\sDate,Contributed.*"
// strBuffer.toString().matches( strValidHeader )
else if ( strBuffer.toString().matches( "LoanX.*" ) ) {
// looks like we got valid data back
fLoop = -9;
}
else {
// this a bad place
// no exception, no error message & no valid header
}
bis.close ();
result.close ();


 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My guess is that you're not at the line you think you're at. Time for some basic debugging: Print out the line.

Also, when you post code, please UseCodeTags(⇐click) so it will be readable, and please provide an SSCCE so we can see in one compact example the entire bit of relevant code, as well as any input it's consuming.
 
kevind duff
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Hey Bartender:

Lets go over this again, if I may be so informal.
As you suggested a simple sample:

I want to trap the first line in strA.
But it ain't working.

I really want to use something like regExA to trap the full line.
But made zero progress on that.

I know you are busy, but please be kind and take a look.

Best

KD



 
Henry Wong
author
Marshal
Pie
Posts: 21190
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kevind duff wrote:
Hey Bartender:

Lets go over this again, if I may be so informal.
As you suggested a simple sample:

I want to trap the first line in strA.
But it ain't working.

I really want to use something like regExA to trap the full line.
But made zero progress on that.

I know you are busy, but please be kind and take a look.

Best

KD


By default, qualifiers do not match end-of-line markers. To match those markers, you need to put the regex engine into DOTALL mode (meaning the dot applies to everything including CR and LF).

Unfortunately, since you are using the string convenience methods, you have to do this from the regex itself -- just add a "(?s)" to the beginning of your regex to put the regex into DOTALL mode.

Henry
 
kevind duff
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Henry thanks for your reply.
"By default, qualifiers do not match end-of-line markers. To match those markers, you need to put the regex engine into DOTALL mode
(meaning the dot applies to everything including CR and LF). "


I do not want to match the end-of-line markers, just want to match up to "Date,Contributed"

Anyway, this worked


but this does NOT work (.* at end of regex has been removed)




And the behavior of the regex expression in Java is different from the behavior in The Regulator
Confusing



thanks again.

KD
 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kevind duff wrote:I do not want to match the end-of-line markers, just want to match up to "Date,Contributed"

Then, almost certainly, you don't want to be using matches().

My suggestion: Have a look at java.util.regex.Pattern and java.util.regex.Matcher. They give you much more flexibility than the String methods that take regexes.

Winston
 
Henry Wong
author
Marshal
Pie
Posts: 21190
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kevind duff wrote:

Henry thanks for your reply.
"By default, qualifiers do not match end-of-line markers. To match those markers, you need to put the regex engine into DOTALL mode
(meaning the dot applies to everything including CR and LF). "


I do not want to match the end-of-line markers, just want to match up to "Date,Contributed"


Go way back to the very first response...

Jeff Verdegan wrote:If you read the docs for String.matches(), you'll see it tries to match the entire input. The regex you have will match a String that starts with "LX_CODE", has 0 to 10 of any character, then has "LX_MESSAGE" and then ends. If you need to match "LX_CODE: 8 LX_MESSAGE: Data Format of xcf not supported, use XML or CSV only" then try adding ".*" at the end of your regex.


The matches() method will still match the *entire* string. Nothing has changed since a few days ago... ... If you want a partial match, you will need to use the regex engine directly -- and use the find() method.

[EDIT: beaten to the answer by three minutes ... :-) ]

Henry
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you trying to apply a regex to the entire input String at one time?


If so, you're almost certainly better off applying it to one line at a time, which is what I initially assumed you were able to do.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic