In an Upload file (Excel sheet ) I had a requirement to validate the amount ( currency ). Below were the validations to be considered for the string containing the amount
To achieve this initially, I used the below logic on the sting <amount>.
Find the length of <first_part>.
Find the length of <second_part>.
Check <first_part> < 13 and <second_part> < 2.
Check <third_part> is initial.
This all validation takes nearly around 25 - 30 lines of code.
But, after knowing the Regular expression concept I revised this 25 - 30 lines of code into just 1 line as below.
FIND ALL OCCURRENCES OF regex '^([0-9]\d{0,12})(\.[0-9]{0,2})?$' IN <amount> MATCH COUNT sy-tabix.
sy-tabix = 1 => its true/correct.
sy-tabix = 0 => its wrong/ incorrect.
Brief Explanation.
Syntax : FIND ALL OCCURRENCES OF regex <Expression> in <String> Match Count <Variable> .
Regular Expression : ^([0-9]\d{0,12})(\.[0-9]{0,2})?$
^ : Matches the starting position within the string
() : The string matched within the parentheses can be recalled later.
[] : Matches a character that is contained within the brackets.
- : Specifies a range.
{} : Number of characters.
\d : Digits.
\. : Mandatory dot.
? : Indicates there is zero or one of the preceding element.
$ : Matches the ending position of the string.
[0-9] : Digits only any number between 0 to 9.
{0,12} : Up to 13 characters allowed only before decimal.
{0,2} : up to 2 characters allowed only after decimal.
Regards
Rounak
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
4 | |
4 | |
2 | |
2 | |
2 | |
2 | |
1 | |
1 | |
1 | |
1 |