-5

I am trying to match a numerical value like xx.xx, xx.x or x.x in a string that may be represented with, or without a asterisk.

*10.00
5.0

But not match singular digits

*1
5m

and replace it with a colored version, but my pattern is not matching the whole element in the string, but multiple parts

Here is what I have tried: https://ideone.com/vZwCzh

String string = "*10.00 5.0 *1 5m";
System.out.println(string.replaceAll("([\\*?0-9.]){3,15}", "<span style=\"color:yellow\">$1</span>")); 

I get

<span style="color:yellow">0</span> <span style="color:yellow">0</span> *1 5m

Instead of

<span style="color:yellow">*10.00</span> <span style="color:yellow">5.0</span> *1 5m
  • 3
    You put a limiting quantifier into a character class, and `{1,13}` is treated as a set of chars. – Wiktor Stribiżew May 18 '17 at 07:42
  • 1
    `[0-9{1,13}.]`? Do you mean `[0-9.]{1,13}`? – Sebastian Proske May 18 '17 at 07:42
  • 1
    I see a wrong use of character classes here. – revo May 18 '17 at 07:42
  • Possible duplicate of [Reference - What does this regex mean?](http://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean) – revo May 18 '17 at 07:43
  • 1
    Thank you Wiktor, and everyone else. My pattern should be: `([\\*?0-9.]){1,15}` Don't know if it's perfect. I added two character to the character count from 13 to 15 for the possible * and . that I didn't think about. Not sure I that was called for. – JordanSasquatchMan May 18 '17 at 07:46
  • @JordanSasquatchMan `([\\*?0-9.]){1,15}` is wrong too. You now put `\\*?` inside the character class. Don't. – Andreas May 18 '17 at 08:51
  • You say to match `double` and only show examples of unsigned values with `.` in them. You then say to not match singular digits. Does that mean that a value like `10` is ok to match? How about `1e0`? Or `-2.5`? Or `NaN`? When you say to match `double`, it sounds like all valid `double` representations, as defined by [Double.valueOf(String s)](https://docs.oracle.com/javase/8/docs/api/java/lang/Double.html#valueOf-java.lang.String-). Is that really what you want? Your own attempt suggests otherwise, so you're not really trying to match `double`. Please edit question and clarify. – Andreas May 18 '17 at 09:00
  • @Andreas I guess I don't mean doubles. I just mean how the `double` are parsed and always display compared to the `int` values displayed as single digits in all available output from the program. I'll remove that. – JordanSasquatchMan May 18 '17 at 09:12
  • 1
    Does that mean you don't want to match `10`? Or `-2.5`? Only unsigned values with a `.` and at least one digit on each side of the `.`? If so, then use `replaceAll("\\*?\\d+\\.\\d+", "$1")` – Andreas May 18 '17 at 09:19
  • `xx.x` or `xx.x` or yes, `x.x` with optional `*` prefix. Like `*10.00 10.0 5.0` – JordanSasquatchMan May 18 '17 at 09:21
  • Also @Andreas I've tried that same basic setup originally, it doesn't even match. You can try easily here: https://www.freeformatter.com/java-regex-tester.html – JordanSasquatchMan May 18 '17 at 09:23
  • 1
    Sure it does: https://ideone.com/10dZD7. Just had to replace `$1` with `$0`, since no capture group was defined. – Andreas May 18 '17 at 09:25
  • Interesting. Both in the program, and in the tester it comes back with no changes, and: `No match found! .matches() method: false .lookingAt() method: false` I am using `$0`. – JordanSasquatchMan May 18 '17 at 09:28
  • Oh I didn't actually run the changes in the program, and apparently Java tester ***only*** uses the `matcher()` and `lookingAt()` methods. – JordanSasquatchMan May 18 '17 at 09:40

1 Answers1

0

You can replace this :
\*?\d(?:\d\.\d\d?|\d?\.\d)
by this :
<span style="color:yellow">$0</span>

Demo here

Gawil
  • 1,091
  • 4
  • 13