Solved

java reg expression string search

Posted on 2007-11-17
11
794 Views
Last Modified: 2008-02-01
This is most likely a simple question, but I don't have much experience with regexp's.  

I have a log file which my java program just read in as a string and I'd like to find all occurences of where it says "Error 00-E1122"  where 00-E1122 would obviously change.  So what I need is to get all of the error code values (but just the error value, not including the word Error), ideally have it in a vector of strings.
0
Comment
Question by:cfans
  • 4
  • 4
  • 3
11 Comments
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
Only the numbers?
Can we see some longer text and show you how to get all the rest too?
Also state the message code format. Is the error code format always: 99-E9999


0
 

Author Comment

by:cfans
Comment Utility
I apologize, this may help... the error code format is always found as: {Error 123ABC} format.  
So you can always be certian that it will be {Error someNumLetterDashSequence} and it will be in the brackets.  All I need is the Number/Letter/Dash sequence.
0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
Some examples for the COMPLETE string of the message would be good and the statements what parts you expect where.

0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
Or can we say: all characters following the word "Error" and one or more blanks is the error number. The error number ends when all alpha, digit and dash chars are eaten or char not from the set of error number chars is found, right?

 
0
 

Author Comment

by:cfans
Comment Utility
Zvonoko, you can say all characters following  "{Error" with 1 or more blank spaces and the error sequence (which is alpha, digit and dash) then ending in "}"
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
And why do you not post an example sting?
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility

>>I have a log file which my java program just read in as a string

Sound somewhat inefficient. You should read line by line. What happens when it gets large?


String error = line.replaceAll("\\{Error ([^}]+)", "$1");

Open in new window

0
 
LVL 86

Accepted Solution

by:
CEHJ earned 150 total points
Comment Utility
Sorry - i must be tiring ;-)


Pattern p = Pattern.compile("\\{Error ([^}]+)");

Matcher m = p.matcher(line);

if (m.find()) System.out.println(m.group(1));

Open in new window

0
 

Author Comment

by:cfans
Comment Utility
CEHJ.. what if there are more than one within a single line?
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Replace if by while
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
:-)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now