0

This is probably an easy question but I haven't been able to figure it out. I want to find the next letter (A to Z) in a string after a certain point in the string. The result I want from below is for the string money to be "$5. 00" but num2 always comes up as -1.

String text = "hello$5. 00Bla bla words that don't matter"

int num1 = text.indexOf('$');
int num2 = text.indexOf("[a-zA-Z]" , num1 + 1); // Always results in -1

String money = text.substring(num1, num2);
a name
  • 19
  • 4
  • 2
    `String::indexOf` does not take regex as its paramater – Scary Wombat Mar 19 '19 at 00:31
  • String indexOf never takes regex as the parameter, it should be another string or char(unicode codepoint) You should be using replaceAll() here. – Laksitha Ranasingha Mar 19 '19 at 00:34
  • See proposed solutions to a similar problem [here](https://stackoverflow.com/questions/4194310/can-java-string-indexof-handle-a-regular-expression-as-a-parameter) – MikaelF Mar 19 '19 at 00:35
  • Your specification could be clearer. It looks like what you want is really the first alphabetic character, since the dot and the space are non-digit characters. – MikaelF Mar 19 '19 at 01:24

3 Answers3

1

To find the first letter following a $ dollar sign, using regex, you can use the following regex:

\$\P{L}*\p{L}

Explanation:

\$       Match a $ dollar sign
\P{L}*   Match 0 or more characters that are not Unicode letters
\p{L}    Match a Unicode letter

The index of the letter is then the last character of the matched substring, i.e. one character before the end() of the match.

Example

String text = "hello$5. 00Bla bla words that don't matter";

Matcher m = Pattern.compile("\\$\\P{L}*\\p{L}").matcher(text);
if (m.find()) {
    int idx = m.end() - 1;
    System.out.println("Letter found at index " + idx + ": '" + text.substring(idx) + "'");
}

Output

Letter found at index 11: 'Bla bla words that don't matter'

UPDATE

It seems the actual question was slightly different than answered above, so to capture the text from $ dollar sign (inclusive) and all following characters up to first letter (exclusive) or end of string, use this regex:

\$\P{L}*

Example

String text = "hello$5. 00Bla bla words that don't matter";

Matcher m = Pattern.compile("\\$\\P{L}*").matcher(text);
if (m.find()) {
    String money = m.group();
    System.out.println("money = \"" + money + "\"");
}

Output

money = "$5. 00"
Andreas
  • 154,647
  • 11
  • 152
  • 247
0

This is untested, as my workstation isn't set up for Java 9, but using that release, you should be able to do this:

String result = text.substring(text.indexOf('$'), text.length())
    .takeWhile(ch -> !Character.isAlphabetic(ch))
    .map(Object::toString).collect(Collectors.joining());

result will evaluate to $5. 00

Note: Stream<T>#takeWhile is a Java 9 feature

MikaelF
  • 3,518
  • 4
  • 20
  • 33
-1

Thanks for the help everyone. I found a way to do this without using regex.

String money = "";
while (!Character.isLetter(text.charAt(num1))) {
  money = money + text.charAt(num1);
  num1++;
}

It might need some work later but it seems to work.

a name
  • 19
  • 4