What may be the regex for allowing 17 digits with or without spaces in-between any number of the characters?
I am using a OCR real-time capture SDK (ABBYY RTR SDK) on Android(, and I'm using a custom data capture (which has to capture 17 digits with or without spaces in-between anywhere)
My regex is currently this:
field.setRegEx( "[0-9]{14,16}" );
This is only able to capture in-line digits (38492039483726473)
But I need it to capture digits in that format, as well as this format - 3849 20394 8372 6473.
How do I go about achieving this?
If it's simply those two formats:
(\d{17}|\d{4}\s\d{5}\s(?:\d{4}\s){2})
Captures a digit 17 times
or
Capture 4 digits followed by a space, then 5 digits followed by another space, and then 4 digits followed by a space twice
Try repeating a digit with optional space:
(\d ?){17}
Demo
An alternative to the answer given by #mrzasa would be to first remove all whitespace from the number string, and then use a simpler regex to check for 17 digits:
String input = "3849 20394 8372 6473";
if (input.replaceAll("\\s+", "").matches("\\d{17}")) {
System.out.println("match");
}
Related
In my filter used into EditText, I want to be sure the user can only set .5 or .0 for decimal values.
Valid values examples:
34.5
34.0
34
Invalid values examples:
34.2
34.8
34.6
I tried this one, but it doesn't work properly: [0-9]*[.]?[0|5]
Thank you very much guys!
You're probably looking for [0-9]*(\.([50][0]*)*)*.
[0-9]*: Any character from 0 to 9, zero or more times [so that just a "." (= 0.0) input is valid]
\.: You need to escape the '.' character, since it usually would mean "any character", and you especifically need the dot there.
[50][0]*: First, either five or zero (once). Second, the 0 character, zero or more times (since 35.50 = 35.5). This also avoids inputs like 35.59 from being valid, since 9 != 0.
([50][0]*)*: This occurrence zero or more times, so that 35., for instance, becomes a valid input (since 35. = 35.0).
(\.([50][0]*)*)*: As for this grouping, it's in order to check for the five or the zero only if there is a decimal dot. It's grouping the dot character and the 5/0 logic together with a star (zero or more times) at the end, so if it doesn't occur, it still matches.
Let me know if this was what you were looking for.
To verify the whole numbers in the examples, you can make the last part optional and use anchors.
^[0-9]+(?:[.][05])?$
^ Start of string
[0-9]+ Match 1+ digits 0-9
(?:[.][05])? Optionally match . and a digit 0 or 5
$ End of string
See a regex demo.
If you want to be able to only type a pattern like that and also accept empty strings or 34. you can repeat the digit 0 or more times, optionally match . and optionally match either 0 or 5.
^[0-9]*[.]?[05]?$
See another regex demo
I have written an Android application to analyze screenshots and extract data for further analysis.
Unfortunately it happens that characters get extracted twice.
For example the value 15.134.567 is detected as one line with elements 15.134 and 4.567.
When I combine the line to get the full value -> 151344567 which is one digit too long, because 4 has been detected twice.
This happens very random, so maybe 1 out of 30 values is detected wrong.
Is there any improvement possible?
e.g. predefined scan area, adjust limits, change to greyscale image, set expected data format ##.###.### ?
Thanks a for for your help!
[example image for OCR][1]
[1]: https://i.stack.imgur.com/PbtSx.jpg
I'm trying to build a component similar to Google Play's credit card adding:
Where the user starts typing the credit card number, and only the credit card images that stay visible, are of the cards that matches part of the credit card number pattern.
For example:
Looking at MasterCard regex: ^5[1-5][0-9]{14}$
If the user type "5", "53", "531", the image of master card should be visible.
I guess it can be done with Pattern and Matcher class, but how?
Using match() method is not good, as it trying to match against all the pattern.
Thanks in advance!
If you want a pattern for live validation, you can use
^5([1-5][0-9]{0,14})?$
See the regex demo.
Note you cannot use it for final string validation, you will have to use your current regex for it.
The main thing about a regex for live input validation is that it allows the next character only if the previous one matches, and that is possible with nested optional groups. Since you have only 3 parts: 1) the first obligatory 5, then 1 to 5 digit, and then any 0 to 14 digits, you may use just one optional group around [1-5][0-9]{0,14} pattern and and make sure you allow zero or up to 14 other digits.
Details:
^ - start of string
5 - obligatory 5
([1-5][0-9]{0,14})? - an optional sequence (1 or 0 occurrences) of:
[1-5] - a digit from 1 to 5
[0-9]{0,14} - any zero to forteen digits
$ - end of string.
Use your pattern for activation the buttonand this for the image
^5[1-5][0-9]{0, 14}$
Ok I'm a massive noob and apart from following lots of tutorials I like to set myself a problem and then try to fix it with an app. Therefore I'm trying to make a little app that'll help me when I'm at work.
Basically it needs to breakdown a 4 character string into it's individual characters and then display them phonetically. So if I (the user) type in 5F9A then it'll display FIVE FOXTROT NINE ALPHA. At work we have an excel spreadsheet that does this all and I'm just trying to reverse engineer it. The spreadsheet itself has multiple stages, it reads the characters, converts them into ASCII and then performs a vlookup on a range of cells where each ASCII code is next to it's phonetic pronunciation. It looks for the number 53 (5 in ASCII) and then looks at the cell next to it which says FIVE.
I've managed to translate any user input into ASCII but I just don't know how to store and access this next set of data. I've been looking into SQLite but that is waaaaaay beyond me at the moment and seems far to complicated for something this simple?
Anyway, I know it's cheating asking for the answer, but maybe a push in the right direction?
The dummy way to do that would be:
Get every letter (char) of the word
Have a switch case that gives you the phonetic equivalent (you will have to do that by hand)
String word = yourWord;
String phonetic;
char currentChar;
for(i=0;i<=word.lenth();i++){
currentChar = word.substring(i, i+1);
phonetic = getPhonetic(currentChar)
}
String getPhonetic(char char){
switch char{
case a:
return alpha;
break;
case b:
....
}
}
Here is an example of what I'm trying to do:
If a phone number is entered as 6123044356 it will display as (612)304-4356. Should a guest hit the backspace key 5 times instead of removing the last 4 numbers entered and the dash, remove the last 5 numbers entered. The phone number at that point would be displayed as (612)30. Should the guest hit the backspace key 3 more times the last 3 numbers displayed would be removed, not the parenthesis. The phone number would then be displayed as 61.
Any suggestions on how to do this?
Look at libphonenumber library. It has AsYouTypeFormatter class in it, which will handle this for you.
Alternatively you can use PhoneNumberFormattingTextWatcher which basically does the same thing. You would use it like so:
editText.addTextChangeListener(new PhoneNumberFormattingTextWatcher());
I would do it just like the dialer does it. That way is most intuitive.
Basically when they are typing, as soon as a number, say 1234567, gets to 7 digits, split it up like 123-4567 for the user. Then when it reaches 10 digits, 123-4567890, split it up like (123)456-7890 for them. When they reach 11 digits, change the format to 1-(234)567-8901.
When they are removing numbers from an 11 digit number, as soon as they hit 10 digits, change back to (123)456-7890. After that, as soon as the number of digits is less than 10, change the format back to 123-456789. Keep it in that format until they reach six digits when you just change it to 123456.
By changing the numbers to and from recognizable forms when a user reaches a certain number of digits, it subtly alerts the user that they have either created a real number, or there are too many or not enough digits for this to be a real number.
I suggest using the following thresholds:
11 digits: 1-(234)567-8901
10 digits: (123)456-7890 (anything less than this looks like 7 digits with digits on the end)
7 digits: 123-4567 (anything less just removes the dash).
I hope that makes sense. I do not think the user should have to input/erase any extra characters like parentheses and dashes. It should all be taken care of by the app.