Hi, I am currently working on a script that will compare two numbers from a file. I am thinking that I need to use a regular expression in order to get the numbers, however I don't know what the regex would be since there are multiple numbers through out the text file im parsing. The sentence that these numbers are in would look like this, "In our tests, downloads using control flows achieved up to 1114 Kbps while downloads using BitTorrent achieved up to 1792 Kbps." Can anyone suggest a regex that would pull those 2 numbers, but not any of the other numbers in the file?

Any and all help/ideas/comments are very appreciated. Also if you need me to provide some more information just let me know.


$lines = file(getcwd().'/data.txt');
foreach( $lines as $line)
    if( preg_match('/^In our tests\D+(\d+)\D+(\d+)/',$line,$matches) )

If there are any matches, then the variable $matches will contain the data you are after.

You are aware that this is the Java forum... ?

Anyway, it depends on how your file is structured. Is the data located in each new line? Or it is in a lengthy one line style? Oh, and what "script"???

You are aware that this is the Java forum... ?

I am now. However, the regex I provided earlier still applies. Here's the java equivalent:

String EXAMPLE_TEST = "In our tests...1234 and another number 4568...done!";
Pattern pattern = Pattern.compile("^In our tests\\D+(\\d+)\\D+(\\d+)");
Matcher matches = pattern.matcher(EXAMPLE_TEST);
if( matches.find() )
    //matches.group(1) has the first number
    //matches.group(2) has the second number
    System.out.println( matches.group(1) + " " + matches.group(2) );
    System.out.println("No Match!");

It could be apply if and only if the file contains exactly "In our tests ... #### ... #### ..." format. If the number has a comma, it broke. If there is new line characters between sentence, I believe it also fails. If the white space between In, our, and tests are more than 1 white space or even a tab, it fails again. That's the reason I asked for the data structure because what he gave us is too narrow.

Thank you hielo! That regex works for me. I am very new to regexs and don't really graps the concept of grouping well yet, but that really helped.

edit: To any moderators or veterans of this forum bored. Is there a way for me to flag this question/thread as being anwsered?

Yes, there's a "Mark Question Solved" at the bottom of the page.

Thanks for being a very helpful forum bored =)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.