?
Solved

Regexp?

Posted on 2004-11-22
6
Medium Priority
?
246 Views
Last Modified: 2010-03-31
I have a file I am reading and I need to read a value, the block of text I am reading is this:

WebRefresh=1 (0 to 10 Minute(s))
AlarmResetMode=0 (0 to 1)
BatteryChemistry=0 (0 to 1)
StringSetup=1 (1 to 2), 40 (1 to 400), 1, 17.0 ( 6.5, 7.0, 8.0, 9.0,12.0,13.5,17.0)
BatteryAHCapacity=100 (100 to 2000)
BalanceBoostTime=3 (3 to 10 Second(s))
CellVoltageLimitMinMax=2.150, 2.400 (2.000 to 2.500 V)
PilotTempLimitMax=95.0 F (85.0 to 120.0 F)
AmbientTempLimitMinMax=50.0, 95.0 F (50.0 to 120.0 F)
ResponseDeviationAlarmLevel=20 (10 to 20 %)
DischargeDeviationAlarmLevel=15 (5 to 20 %)
RippleCurrentAlarmLevel=5 (0 to 2000)

now, what I do now, is look for the parameter then split it on the = sign and I need to return everything after the = sign up to the ( how would I go about this?

I am horrible with expressions.

This is what I have now that splits the = sign.

                String setLine;
                String changeValue;
                while (in.available() != 0) {
                    // Print file line to screen
                    System.out.println(""+in.readLine()+ "\n");
                    setLine = in.readLine();
                    if (setLine.indexOf("DischargeDeviationAlarmLevel") != -1) {
                        String[] line = setLine.split("=");
                        changeValue = line[1].substring(1, 2);
                        System.out.println(changeValue);
                        break;
                    }
                }

Of course code syntax suggestions always welcome as well.
0
Comment
Question by:k41d3n
6 Comments
 
LVL 2

Author Comment

by:k41d3n
ID: 12646448
And the line

 changeValue = line[1].substring(1, 2);

is actually

 changeValue = line[1];
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12646470
You can use this to read that file:

Properties props = new Properties();
props.load(new FileInputStream(yourFile));
String value = props.getProperty("ResponseDeviationAlarmLevel"); // gets "20 (10 to 20 %)"
0
 
LVL 2

Author Comment

by:k41d3n
ID: 12646538
I ended up using:

                String setLine;
                String changeValue;
                while (in.available() != 0) {
                    // Print file line to screen
                    System.out.println(""+in.readLine()+ "\n");
                    setLine = in.readLine();
                    if (setLine.indexOf("DischargeDeviationAlarmLevel") != -1) {
                        String[] line = setLine.split("=");
                        changeValue = line[1].substring(0, line[1].indexOf("("));
                        System.out.println(changeValue);
                        break;
                    }
                }
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 3

Expert Comment

by:vikraman_b
ID: 12646669
hi
one example to extract the e-mail addres from a string like "dfd gfdg dgdgd hje@juf.com ejfek" using RegExp
<html>
<head>
<script>
function findEmail(str) {
    var regex=/[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]/;
    var m=str.match(regex);
    alert(m);
}
</script>
</head>
<body>

<button onclick="javascript:findEmail('the email address Myemail@hotmail.com could be anywhere');">Find the Email</button>

</body>
</html>

or

inputs.match(/(\w+)[@](\w+)[.](\w+)/g,"");


u can try the same logic for ur case
0
 
LVL 92

Expert Comment

by:objects
ID: 12648935
try something like:

                String setLine = null;
                String changeValue = null;
                while (null!=(setLine=in.readLine())) {
                    // Print file line to screen
                    System.out.println(""+setLine+ "\n");
                     if (setLine.indexOf("DischargeDeviationAlarmLevel") != -1) {
                        changeValue = setLine.split("=")[1].split(" ")[0];
//                        or perhaps depending on possible format of value
//                        changeValue = setLine.split("=")[1].split("\\(")[0];
                        System.out.println(changeValue);
                    }
                }
0
 
LVL 1

Accepted Solution

by:
Ortokobold earned 2000 total points
ID: 12664072
String setLine;

// pattern below: I'm not quite sure, if there has to be "\\" or just "\"
Pattern p = Pattern.compile("(\\w+)=(\\w+)\\s.*");

while ((setLine=in.readLine()) != null)
{
  // Print line to screen
  System.out.println(setLine);
  Matcher m = p.matcher(setLine);
 
  if (m.matches())
  {
    String name = m.group(1);
    String value = m.group(2);
    System.out.println("Name: " + name + " Value: " + value);
  }
}
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

839 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