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.

4 Years
Discussion Span
Last Post by deadsolo
Featured Replies


$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.


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?

Edited by deadsolo: additional thoughts

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.