0

Looking at this solution to an answer that was made for an EditText field in Android Studio, I am wondering what this string means and how I can break it down to understand and interpret the parts that I need.

I have referred this:Android Money Input with fixed decimal

I pulled this from Zds and Ninjasense's answer

if(!s.toString().matches("^\\$(\\d{1,3}(\\,\\d{3})*|(\\d+))(\\.\\d{2})?$"))

What I'm trying to figure out is how to remove the $ sign that is attached to the EditText so that I can use the number and make some calculations to it.

The whole code I'm currently using in my project is:

@Override
public void onTextChanged(CharSequence s, int start,
        int before, int count) {
    if(!s.toString().matches("^\\$(\\d{1,3}(\\,\\d{3})*|(\\d+))(\\.\\d{2})?$"))
    {
        String userInput= ""+s.toString().replaceAll("[^\\d]", "");
        StringBuilder cashAmountBuilder = new StringBuilder(userInput);

        while (cashAmountBuilder.length() > 3 && cashAmountBuilder.charAt(0) == '0') {
            cashAmountBuilder.deleteCharAt(0);
        }
        while (cashAmountBuilder.length() < 3) {
            cashAmountBuilder.insert(0, '0');
        }
        cashAmountBuilder.insert(cashAmountBuilder.length()-2, '.');
        cashAmountBuilder.insert(0, '$');

        cashAmountEdit.setText(cashAmountBuilder.toString());
        cashAmountEdit.setTextKeepState(cashAmountBuilder.toString());
        Selection.setSelection(cashAmountEdit.getText(), cashAmountBuilder.toString().length());
    }
}
Community
  • 1
  • 1
HarrisonT
  • 41
  • 5

1 Answers1

0

Check out this link for visualizing the regular expression in your string.

The check mainly relies on digits \d’ to be found a fixed{3}or ranged{1,3}or unlimited+number of times, allowing,and.` to be preseent at specific locations.

regexvisual

Jan
  • 13,738
  • 3
  • 30
  • 55