2

Is there a number wildcard character in java? I'm opening a file and looking at a list of data and I need to differentiate between three pieces of information that start with "M". However, one of them has numbers directly following it and the other two have letters that follow. I was wondering if there was a way to check if there was a number after the letter with a wildcard character. I'm sure you could do this with ASCII, but I also am unsure of how to execute that.

EDIT: I'm still having issues, so here is my code.

import java.io.*;
import java.util.*;
import java.util.regex.*;

public class addSevTest{
    public static void main(String[] args) throws IOException{
        FileReader fr = new FileReader("output6.txt");
        BufferedReader br = new BufferedReader(fr);
        String line;
        Pattern pattern = Pattern.compile(br.readLine());
        Matcher matcher = pattern.matcher(br.readLine());

    List<String> list = new ArrayList<String>();
    while ((line = br.readLine()) != null){
        if(line.contains("100%") || line.contains("70%") || matcher.find("[.][1-9]")){
            list.add(line);
            list.add("     2");
            list.add("\n");
            //System.out.println('Using String matches method: '+line.matches('.M'));
        }else if(line.startsWith("MDRALM")){
            list.add(line);
            list.add("     3");
            list.add("\n");
        }else if(line.startsWith("SOL") || line.startsWith("I/O") || line.startsWith("AH") || line.startsWith("LT")){
            continue;
        }else{
            list.add(line);
            list.add("     1");
            list.add("\n");
        }
    }

    /*while ((line = br.readLine()) != null){
        if(line.contains("CP")){
            list.add(line);
            list.add("\n");
        }
    }*/
    br.close();

    FileWriter writer = new FileWriter("addSevTest_O.txt"); 
        for(String str: list){
            writer.write(str);
        }
        writer.close();
}

}

11
  • 6
    Are you looking for Regex? Commented Sep 5, 2013 at 14:41
  • 2
    Regular expressions. See the API documentation of the package java.util.regex. Commented Sep 5, 2013 at 14:42
  • 2
    You're looking for a Regex. You can use Pattern and Matcher classes along with your Regex to evaluate the String. Commented Sep 5, 2013 at 14:42
  • 2
    Did we all mention Regex, already? :) Commented Sep 5, 2013 at 14:42
  • 2
    Regex is a language on it's own. Whether you're programming ruby, javascript, java, or even C.. Regex, while will have little differences between languages... it is at it's core, a language in and of itself. Have fun with it. Commented Sep 5, 2013 at 14:57

1 Answer 1

5

You'd be best off using some simple regular expressions.
I found some basic tutorials you can skim through for the basics here:

And a couple of tools to help you on your journey:

EDIT

In your added code, try replacing this:

    if(line.contains("100%") || line.contains("70%") || matcher.find("[.][1-9]"))

with this:

    if(line.contains("100%") || line.contains("70%") || line.matches("M[1-9]+.*"))

The M matches the first letter of the line. [1-9] matches the digits, with the + meaning one or more. .* means zero or more additional characters following the number will also match.

The Pattern/Matcher stuff you've got here is overkill for your purposes.

Sign up to request clarification or add additional context in comments.

7 Comments

@CinCity add the code as an edit to your original question and we'll look
@CinCity To start, pattern.compile should take a regular expression string.
Secondly, using Pattern and Matcher might be overkill here... a simple string.matches(regex) would probably serve your purposes - something like string.toLowerCase().matches("M[0-9].*") might do the trick
Oh, and this regex - "[.][1-9]" what are you trying to match with [.]? Because that's going to match the literal character '.'
Totally works, many thanks my good sir. I'm going to have to study the regex harder.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.