1

If a string has no digits in it, I would like to replace it with "Invalid" using Java (shorthand); this is to be an expression in the tMap component in Talend Open Studio

Here are some examples of desired outcomes:

Here are valid entries that should stay the same and remain unchanged, they are valid:

  1. "1234"

  2. "123-456-7890"

  3. "(123) 456-7890"

Here are some values that are to replaced with "Invalid":

  1. "asdf"

  2. "(xxx) xxx-xxxx"

  3. "() -() -()"

  4. "***-***-****"

  5. "- -"

Here's what I've tried so far:

myTable.myField.replaceAll("[0-9]","").isEmpty() ? "Invalid" : myTable.myField

But it's not working out, at least not in Talend

Drewdavid
  • 3,071
  • 7
  • 29
  • 53
  • 1
    you have to set it so something: `myTable.myField = myTable.myField.replaceAll("[0-9]","").length() == 0 ? "Invalid" : myTable.myField;` – Jason Dec 06 '13 at 21:26
  • http://stackoverflow.com/questions/7013590/how-to-use-replacechar-char-to-replace-all-instances-of-character-b-with-noth Check this out, it may help – Eric J. Dec 06 '13 at 21:38
  • `isEmpty()` is shortcut for `length() == 0`. To improve your question provide examples of input and expected output. – Pshemo Dec 06 '13 at 22:16
  • From your examples seems that you are looking for `replaceAll("^[^0-9]+$", "invalid")` which is part of @JoshF answer. – Pshemo Dec 06 '13 at 23:31

3 Answers3

3

Just replace any string that's all digits, like this:

myString.replaceAll("^[0-9]+$", "invalid");

According to comments, OP wants to replace any strings that don't have digits in them. here's the answer for that:

myString.replaceAll("^[^0-9]+$", "invalid");

Also, this will replace strings that do have some digits:

myString.replaceAll("^.*[0-9].*$", "invalid");
Josh T
  • 564
  • 3
  • 12
  • Strings are immutable, you need to reassign it with result of replacement. – Pshemo Dec 06 '13 at 21:38
  • Hi, I'm looking to replace strings that have no digits with "Invalid", not strings that are all digits – Drewdavid Dec 06 '13 at 21:40
  • 1
    @Drewdavid `... if removing all of the digits produces an empty string` is not the same as `replace strings that have no digits with "Invalid"`. Which one is it? – Pshemo Dec 06 '13 at 21:45
  • @Drewdavid I answered the question you wrote, but if you want all strings to contain at least one digit use this : ^.*[0-9].*$ : sorry, the question just wasn't clear – Josh T Dec 06 '13 at 21:46
  • @Pshemo good spot, I fixed it. I think this is what he wants now. – Josh T Dec 06 '13 at 21:52
  • @Drewdavid Are any of these what you're looking for? Ex: 1) replaces "1234", 2) replaces "asdf" but not "asdf1", and 3) replaces "asdf1" but not "asdf" – Josh T Dec 06 '13 at 22:06
  • Hi, thanks I updated my question with examples to help make it more clear :) – Drewdavid Dec 06 '13 at 22:19
1

you have to set it so something:

myTable.myField = myTable.myField.replaceAll("[0-9]","").length() == 0 ? "Invalid" : myTable.myField;

However in this case I would use an if statement:

if (myTable.myField.replaceAll("[0-9]","").length() == 0){
    myTable.myField = new String("Invalid");
}
Jason
  • 13,563
  • 15
  • 74
  • 125
1

Why don't you use a very simple Pattern for that?

For instance:

String foo = "abc123";
String bar = "abcdef";
Pattern p = Pattern.compile("\\d");
System.out.println(p.matcher(foo).find());
System.out.println(p.matcher(bar).find());

Output:

true
false

So in your case:

if (p.matcher(myTable.myField).find()) {
    // I'm just making the method "setText" up - replace with actual method
    myTable.myField.setText("Invalid");
}
Mena
  • 47,782
  • 11
  • 87
  • 106