5

I have a (large) string file in my application which contains a series of random character [a-Z] and [0-9] but also ";","/", "?", ":" and "@". I would like my application to tell me the nearest position where 6 digits are displayed consecutively ( like "105487" or "558463").

What would be the best way to achieve this? Thank you for looking into this.

Gene
  • 46,253
  • 4
  • 58
  • 96
DVPeer
  • 63
  • 3

3 Answers3

4

You could use regex.

String regex = "(\\d{6})";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(YOU STRING HERE);
// Check all occurrences
while (matcher.find()) {
    System.out.print("Start index: " + matcher.start());
    System.out.print(" End index: " + matcher.end());
    System.out.println(" Found: " + matcher.group());
}

This would do the job.

(Code sample from here)

Community
  • 1
  • 1
La bla bla
  • 8,558
  • 13
  • 60
  • 109
3

Use Character.isDigit while iterating the string's characters and then count a number up until you have found 6 consecutive numbers or reset it if the sequence breaks. Keep track of the index and you can simply calculate the nearest position by subtraction.

This is not very efficient, but I think O(n) is sufficient if the strings are not too large. For optimization take a look on what Luiggi Mendoza has suggested in the comments.

Konrad Reiche
  • 27,743
  • 15
  • 106
  • 143
  • Thank you for your answer. I thought this would be the only solution, but i was hoping that java had a more efficient way of doing this. I will look into this. – DVPeer Oct 01 '12 at 21:30
  • @user1637234 you could use a regular expression like Lablabla has posted. – Luiggi Mendoza Oct 01 '12 at 21:30
  • 2
    You can optimize this: if you find a digit in position X, then check the character in position X + 5, if that character is not a a digit, then start searching from here. If it's a digit, then go back (X+4, X+3...) until you find a non-digit and start again from the least position. – Luiggi Mendoza Oct 01 '12 at 21:34
  • @gtgaxiola yeah, one of my String matching favorite algorithms when I can't rely on RegEx. – Luiggi Mendoza Oct 01 '12 at 21:38
1

An effective approach would be to iterate the characters of the string and test if each is a digit. Upon finding a match continue to look for the rest of the sequence. Something like

int nDigits=0, i = 0;
CharacterIterator it = new StringCharacterIterator("very long string123456");
for (char ch=it.first(); ch != CharacterIterator.DONE; ch=it.next()) {
  i++;
  nDigits = (ch.isDigit() ? nDigits++ : 0);
  if (nDigits == 5) {
      // DONE. Position is "i"
  }
}
Johan Sjöberg
  • 47,929
  • 21
  • 130
  • 148