3

I am trying to write a program in which I have to do comparison between a list of strings with a template (which is essentially a string). I am not sure what is the term used but it is going to be more of log scraping program if that helps.

Input String Examples:

  1. Hello World this is me
  2. Hello Strange World this is a someone
  3. This is a test file
  4. Hello World this is a bot

To be compared against

a. This is a ? file

b. Hello World this is ?

The idea is to match input statements (1-4) against template strings (a-b) and if they match then I need to act on them. Like 1 & 4 match sentence b but 2 does not.

Thanks in advance for help/directions.

Rox
  • 33
  • 1
  • 5

1 Answers1

1

Change your ? to .* and you've got a regex:

String input = "Hello World this is me";

if (input.matches("Hello World this is .*"))
    // true

etc

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Sorry if I gave a bad example but I might have the ***?*** strings in middle of a sentence and I am little worried about the punctuation coming in the log statements – Rox Feb 16 '13 at 03:27
  • @Rox - You can deal with punctuation using regexes. Read http://www.regular-expressions.info/ and http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html – Stephen C Feb 16 '13 at 03:34
  • @StephenC - Thanks I think that would help. Cheers ! – Rox Feb 16 '13 at 03:37