Regular expression for mobile number vaidation? - java

I have following regular expression for following mobile numbers:
^(([+]|[0]{2})([\\d]{1,3})([\\s-]{0,1}))?([\\d]{10})$
Valid numbers are:
+123-9854875847
00123 9854875847
+123 9854875847
9878757845
Above expression will not validate if user enter 9 or 11 digit mobile number but if user enter 9 or 11 digit number with +123 or +91 respectively then it is getting validate because in this part of expression ([\\d]{1,3}) last two digits are optional.
So, any way to make this part ([\\s-]{0,1}))?([\\d]{10}) not to get combine with this part ([\\d]{1,3})?

The question is somewhat unclear, but I presume you want to split the number and the country code.
This is quite easy to do by extracting groups. group(i) is the i-th thing in brackets.
I also applied these simplifications: [\\d] = \\d, {0,1} = ?, [+] = \\+, [0]{2} = 00.
Code:
String regex = "^((\\+|00)(\\d{1,3})[\\s-]?)?(\\d{10})$";
String str = "+123-9854875847";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
if (m.matches())
{
System.out.println("Country = " + m.group(3));
System.out.println("Data = " + m.group(4));
}
Output:
Country = 123
Data = 9854875847
Alternative using non-matching groups (?:): (so you can use group(1) and group(2))
String regex = "^(?:(?:\\+|00)(\\d{1,3})[\\s-]?)?(\\d{10})$";
String str = "+123-9854875847";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
if (m.matches())
{
System.out.println("Country = " + m.group(1));
System.out.println("Data = " + m.group(2));
}
Reference.
Related test.

As long as the extension is always separated from the rest of the phone number, your regex will work fine. If there is no such separation, there is no way to correctly validate a phone number.
Also keep in mind that both extensions and phone numbers can vary in length from country to country, so there is no regex that will solve all cases. If you can produce a list of allowed extensions, you can work that into the regex and get better matches, but for many groups of arbitrary length of digits you will get many wrong matches.
I have simplified your regex a bit, so oyu can see #Dukeling's suggestions in practice. Your regex on top, mine on the bottom.
^(([+]|[0]{2})([\\d]{1,3})([\\s-]{0,1}))?([\\d]{10})$
^( (\\+|00) \\d{1,3} [\\s-]?)? \\d{10} $

try {
String mobile_number="india number +919979045000\n" +
"india number 9979045000\n" +
"china number +86 591 2123654\n" +
"Brazil number +55 79 2012345\n" +
"it is test all string get mobile number all country"+
"Ezipt +20 10 1234567\n" +
"France +33 123456789\n" +
"Hong Kong +852 1234 5456\n" +
"Mexico +52 55 12345678"+
"thanks";
Pattern p = Pattern.compile("\\(?\\+[0-9]{1,3}\\)? ?-?[0-9]{1,3} ?-?[0-9]{3,5} ?-?[0-9]{5}( ?-?[0-9]{3})? ?(\\w{1,10}\\s?\\d{1,6})?");
List<String> numbers = new ArrayList<String>();
//mobile_number= mobile_number.replaceAll("\\-", "");
Matcher m = p.matcher("" + mobile_number);
while (m.find()) {
numbers.add(m.group());
}
p = Pattern.compile("\\(?\\+[0-9]{1,3}\\)? ?-?[0-9]{1,3} ?-?[0-9]{3,5} ?-?[0-9]{4}( ?-?[0-9]{3})? ?(\\w{1,10}\\s?\\d{1,6})?");
m = p.matcher("" + mobile_number);
while (m.find()) {
numbers.add(m.group());
}
p = Pattern.compile("((?:|\\+)([0-9]{5})(?: |\\-)(0\\d|\\([0-9]{5}\\)|[1-9]{0,5}))");
m = p.matcher("" + mobile_number);
while (m.find()) {
numbers.add(m.group());
}
p = Pattern.compile("[0-9]{10}|\\(?\\+[0-9]{1,3}\\)?-?[0-9]{3,5} ?-?[0-9]{4}?");
m = p.matcher("" + mobile_number);
while (m.find()) {
numbers.add(m.group());
}
String numberArray=numbers.toString();
System.out.print(""+numberArray);
// final result
/* [+919979045000, +86 591 2123654, +33 123456789, +52 55 12345678, +919979045000, +86 591 2123654, +55 79 2012345, +20 10 1234567, +33 123456789, +852 1234 5456, +52 55 12345678, +919979045000, 9979045000] */
} catch (Exception e) {
e.printStackTrace();
}

Best way to take input in two parts i.e country code and mobile number.
In that case you can easily validate it (both country code and mobile number) with regex.

Related

How to recover integers?

I get a string and I have to retrieve the values
Je pense que nous devons utiliser le ".slit"
if (stringReceived.contains("ID")&& stringReceived.contains("Value")) {
here is my character string:
I/RECEIVER: [1/1/0 3
I/RECEIVER: :32:11]
I/RECEIVER: Timestam
I/RECEIVER: p=946697
I/RECEIVER: 531 ID=4
I/RECEIVER: 3 Value=
I/RECEIVER: 18
I receive the value 1 byte by 1 byte.
I would like to recover the value of Timestamp, Id and Value..
You can also use regex for that. Something like:
String example="[11/2/19 9:48:25] Timestamp=1549878505 ID=4 Value=2475";
Pattern pattern=Pattern.compile(".*Timestamp=(\\d+).*ID=(\\d+).*Value=(\\d+)");
Matcher matcher = pattern.matcher(example);
while(matcher.find()) {
System.out.println("Timestamp is:" + matcher.group(1));
System.out.println("Id is:" + matcher.group(2));
System.out.println("Value is:" + matcher.group(3));
}
If the order of tokens can be different (for example ID can come before Timestamp) you can also do it. But since it looks like log which is probably structured I doubt you will need to.
First [11/2/19 9:48:25] seems unnecessary so let's remove it by jumping right into "Timestamp".
Using indexOf(), we can find where Timestamp starts.
// "Timestamp=1549878505 ID=4 Value=2475"
line = line.substring(line.indexOf("Timestamp"));
Since each string is separated by space, we can split it.
// ["Timestamp=1549878505", "ID=4" ,"Value=2475"]
line.split(" ");
Now for each tokens, we can substring it using index of '=' and parse it into string.
for(String token: line.split(" ")) {
int v = Integer.parseInt(token.substring(token.indexOf('=') + 1));
System.out.println(v);
}
Hope that helps :)
String text = "Timestamp=1549878505 ID=4 Value=2475";
Pattern p = Pattern.compile("ID=(\\d)");
Matcher m = p.matcher(text);
if (m.find()) {
System.out.println(m.group(1));
}
output
4
A simple regex is also an option:
private int fromString(String data, String key) {
Pattern pattern = Pattern.compile(key + "=(\\d*)");
Matcher matcher = pattern.matcher(data);
if (matcher.find()) {
return Integer.parseInt(matcher.group(1));
}
return -1;
}
private void test(String data, String key) {
System.out.println(key + " = " + fromString(data, key));
}
private void test() {
String test = "[11/2/19 9:48:25] Timestamp=1549878505 ID=4 Value=2475";
test(test, "Timestamp");
test(test, "ID");
test(test, "Value");
}
prints:
Timestamp = 1549878505
ID = 4
Value = 2475
You can try that:
String txt= "[11/2/19 9:48:25] Timestamp=1549878505 ID=4 Value=2475";
String re1= ".*?\\d+.*?\\d+.*?\\d+.*?\\d+.*?\\d+.*?\\d+.*?(\\d+).*?(\\d+).*?(\\d+)";
Pattern p = Pattern.compile(re1,Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
Matcher m = p.matcher(txt);
if (m.find())
{
String int1=m.group(1);
String int2=m.group(2);
String int3=m.group(3);
System.out.print("("+int1+")"+"("+int2+")"+"("+int3+")"+"\n");
}
Use below code, You will find your timestamp at index 0, id at 1 and value at 2 in List.
Pattern pattern = Pattern.compile("=\\d+");
Matcher matcher = pattern.matcher(stringToMatch);
final List<String> matches = new ArrayList<>();
while (matcher.find()) {
String ans = matcher.group(0);
matches.add(ans.substring(1, ans.length()));
}
Explaining the regex
= matches the character = literally
\d* matches a digit (equal to [0-9])
* Quantifier — Matches between zero and unlimited times, as many times as possible

match a string with text and store the next text

I have a string which contains some text (in Greek language) which was extracted from a pdf.
How can I found a particular text lets say id.name: 123 and then store the number 123?
You can find using a regular expression:
String s = "Έχω ένα string που περιέχει κάποιο κείμενο ( στην ελληνική γλώσσα ), "
+ "το οποίο εξήχθη από ένα PDF .\nΠως μπορώ να ιδρύσω ένα συγκεκριμένο κείμενο "
+ "ας πούμε id.name : 123 και στη συνέχεια να αποθηκεύσετε τον αριθμό 123";
Pattern p = Pattern.compile("id\\.name \\: (\\d+)");
Matcher m = p.matcher(s);
if(m.find()){
System.out.println(m.group(1));
}
Regards.
there are many ways to do it, you can try regular expressions,
for instance let's suppose we have a string call s1 that contain "today is monday" and we can find the word monday, you can do that by:
String matcher = "today is monday";
Pattern p2 = Pattern.compile(".*monday.*");
Matcher m2 = p2.matcher(matcher);
boolean b2 = m2.matches();
if(b2 == true)
{
System.out.println(p2 + " found");
}
else
{
System.out.println(p2 + "no found");
}
}

Certain strings that should be found by a working Regex are missed, and I need help identifying why

I have a set of strings, which I cycle through, checking those against the following set of regex, to try and separate the first small section from the rest of the string. The regex works in almost all cases, but unfortunately I have no idea why it fails occasionally. I’ve been using Pattern Matcher to print out the string, if the pattern is found.
Two example working strings:
98. SORGHUM Moench - Millets Annuals or rhizomatous perennials; inflorescence …
99. MISCANTHUS Andersson - Silver-grasses Rhizomatous perennials; inflorescence …
Two example failed strings:
100. ZEA L. - Maize Annuals; male and female inflorescences separate, the …
26. POA L. (Parodiochloa C.E. Hubb.) - Meadow-grasses Annuals or perennials with or without stolons or rhizomes; sheaths overlapping or some …
Regex’s used so far:
Pattern endOfGenus = Pattern.compile("(?<=(^\\d+\\. " + genusNames[l].toUpperCase() + "))");
Pattern endOfGenusTwo = Pattern.compile("(?<=(^\\d+" + genusNames[l].toUpperCase() + "))");
Pattern endOfGenusThree = Pattern.compile("(?<=(\\d+\\. " + genusNames[l] + "))");
Pattern endOfGenusFour = Pattern.compile("(?<=(\\d+" + genusNames[l] + "))");
Pattern endOfGenusFive = Pattern.compile("(?<=(\\. " + genusNames[l] + "))");
The first of these is the one thats producing the reliable results so far.
Example Code
Pattern endOfGenus = Pattern.compile("(?<=(^\\d+\\. " + genusNames[l].toUpperCase() + "))");
Matcher endOfGenusFinder = endOfGenus.matcher(descriptionPartBits[b]);
if (endOfGenusFinder.find()) {
System.out.print(descriptionPartBits[b] + ":- ");
System.out.print(genusNames[l] + "\n");
String[] genusNameBits = descriptionPartBits[b].split("(?<=(^\\d+\\. " + genusNames[l].toUpperCase() + "))");
}
Desired Output. This is what is produced by strings that work. Strings that don't work simply don't appear in the output:
98. SORGHUM Moench - Millets Annuals or rhizomatous perennials:- Sorghum
99. MISCANTHUS Andersson - Silver-grasses Rhizomatous perennials:- Miscanthus
From regex tutorial:
Lookahead and lookbehind, collectively called "lookaround", are
zero-length assertions just like the start and end of line, and start
and end of word anchors explained earlier in this tutorial.
Lookahead and lookbehind only return true or false.
So I changed your code example:
Pattern endOfGenus = Pattern.compile("(?<=(^\\d+\\. ZEA L))(.+)$");
// Matcher matcher = endOfGenus.matcher("98. SORGHUM Moench - Millets Annuals or rhizomatous perennials; inflorescence …");
Matcher matcher = endOfGenus.matcher("100. ZEA L. - Maize Annuals; male and female inflorescences separate, the …");
while (matcher.find()) {
String group1 = matcher.group(1);
String group2 = matcher.group(2);
System.out.println("group1=" + group1);
System.out.println("group2=" + group2);
}
Group 1 is matched by (^\\d+\\. ZEA L). Group 2 is matched by (.+).

regex- Extracting in different strings

I have this String :
Date Description Amount Price Charge Shares Owned
04/30/13 INCOME REINVEST 0.0245 $24.66 $12.34 1.998 1,008.369
05/31/13 INCOME REINVEST 0.0228 $22.99 $12.22 1.881 1,010.250
06/28/13 INCOME REINVEST 0.0224 $22.63 $11.97 1.891 1,012.141
I want to extract The dates in a string say "matchedDate" similarly description which in this case are "INCOME REINVEST", "INCOME REINVEST" "INCOME REINVEST"
Amount in a array which happen to be : "0.0245","0.0228","0.0224"
Price in a array :"24.66", "22.99", "22.63"
Charge in a array :"12.34","12.22","11.97"
Shares in a array :"1.998","1.881","1.891"
I don't need the last part "Owned" that corresponds to 1,008.369,1,010.250 and 1,012.141
So far I am able to successfully extract dates by this:
String regex="[0-9]{2}/[0-9]{2}/[0-9]{2}";
Pattern dateMatch = Pattern.compile(regex);
Matcher m = dateMatch.matcher(regString);
while (m.find()) {
String[] matchedDate=new String[] {m.group()};
for(int count=0;count<matchedDate.length;count++){
sysout(matchedDate[count]
}
regString being the string i am trying to do a match on i.e the table i explained in the first block.
I don't need the $ sign's so we can store the numbers in integer arrays. I think we have to identify some kind of pattern of spaces and dollar to do this.
Any help would be appreciated
This should match the parts you need:
(\d{1,2}/\d{1,2}/\d{1,2}).+?([\d.]+)\s\$(\S+)\s\$(\S+)\s(\S+)
Explained:
(\d{1,2}/\d{1,2}/\d{1,2}) - capture date
.+? - match anything up to next number
([\d.]+)\s - capture Amount but match space following it
$(\S+)\s - capture Price but match space following it
$(\S+)\s - capture Charge but match space following it
(\S+) - capture Shares
String regString = "04/30/13 INCOME REINVEST 0.0245 $24.66 $12.34 1.998 1,008.36";
String regex="([0-9]{2}/[0-9]{2}/[0-9]{2})\\s*([\\w ]+)\\s*(\\d+(\\.\\d+)?)\\s*\\$(\\d+(\\.\\d+)?)\\s*\\$(\\d+(\\.\\d+)?)\\s*(\\d+(\\.\\d+)?)\\s*(\\d+(,\\d{3})*(\\.\\d+)?)";
Pattern match = Pattern.compile(regex);
Matcher m = match.matcher(regString);
while (m.find()) {
System.out.println(m.group(1)); //04/30/13
System.out.println(m.group(2)); //INCOME REINVEST
System.out.println(m.group(3)); //0.0245
System.out.println(m.group(5)); //24.66
System.out.println(m.group(7)); //12.34
System.out.println(m.group(9)); //1.998
System.out.println(m.group(11)); //1,008.86
}
Demo
Regex Breakdown:
([0-9]{2}/[0-9]{2}/[0-9]{2}) - Your date regex.
([\\w ]+) - Description - 1+ Word characters and spaces.
(\\d+(\\.\\d+)?) (used 4 times) - Amount, Price, Charge, Shares - 1+ number potentially followed by a . and at least 1 more number.
(\\d+(,\\d{3})*(\\.\\d+)?) - 1+ number, followed potentially by sequences of a , and 3 numbers, followed potentially by a . and at least 1 more number.
String r = "([0-9]{2}/[0-9]{2}/[0-9]{2}).+?\\$((?:(?:\\d+|\\d+,\\d+)\\.\\d+\\s\\$?){3})";
String list = "04/30/13 INCOME REINVEST 0.0245 $24.66 $12.34 1.998 1,008.369";
Matcher m = Pattern.compile(r).matcher(list);
while (m.find())
{
String myData = m.group(1) + " " + m.group(2).replace("$", "");
String[] data = myData.split(" ");
for(String s : data)
System.out.println(s);
}
Outputs:
04/30/13
24.66
12.34
1.998
.+?\\$: non-greedy to ensure that we don't take a '$'--basically skips everything until '$'
((?:(?:\\d+|\\d+,\\d+)\\.\\d+\\s\\$?){3} uses a capturing group to get the three numbers of interest, but with one of the '$', which is removed via .replace() You could do this with .replace(), but the expression would be fairly long.
(?:\\d+|\\d+,\\d+) says "group, but do not capture" a number or #,#
\\.\\d+\\s\\$? says a '.' followed by a #, followed by whitespace and an optional '$'
Here's a general tutorial on Regular Expressions. Here's the section on capturing groups. Good luck!
This should give you what you need and it will also run for any number of similar records on your input string ...
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
private static Pattern PATTERN = Pattern.compile("([0-9]{2}/[0-9]{2}/[0-9]{2})\\s+([a-zA-Z]+\\s[a-zA-Z]+)\\s+(\\d{1}\\.\\d{0,4})\\s+\\$(\\d{1,2}\\.\\d{0,2})\\s+\\$(\\d{1,2}\\.\\d{0,2})\\s+(\\d{1,2}\\.\\d{0,3})\\s+");
public static void main(String a[] ) {
String regString = "04/30/13 INCOME REINVEST 0.0245 $24.66 $12.34 1.998 1,008.369 " +
"05/31/13 INCOME REINVEST 0.0228 $22.99 $12.22 1.881 1,010.250 " +
"06/28/13 INCOME REINVEST 0.0224 $22.63 $11.97 1.891 1,012.141 ";
ArrayList<String> date = new ArrayList<String>();
ArrayList<String> desc = new ArrayList<String>();
ArrayList<String> amt = new ArrayList<String>();
ArrayList<String> price = new ArrayList<String>();
ArrayList<String> charge = new ArrayList<String>();
ArrayList<String> share = new ArrayList<String>();
Matcher m = PATTERN.matcher(regString);
while(m.find()) {
date.add(m.group(1));
desc.add(m.group(2));
amt.add(m.group(3));
price.add(m.group(4));
charge.add(m.group(5));
share.add(m.group(6));
}
System.out.println("DATE : " + date.toString());
System.out.println("DESC : " + desc.toString());
System.out.println("AMOUNT : " + amt.toString());
System.out.println("PRICE : " + price.toString());
System.out.println("CHARGE : " + charge.toString());
System.out.println("SHARES : " + share.toString());
}
}
The output of the above program is as below,
DATE : [04/30/13, 05/31/13, 06/28/13]
DESC : [INCOME REINVEST, INCOME REINVEST, INCOME REINVEST]
AMOUNT : [0.0245, 0.0228, 0.0224]
PRICE : [24.66, 22.99, 22.63]
CHARGE : [12.34, 12.22, 11.97]
SHARES : [1.998, 1.881, 1.891]

Regex for floor in address

I have this regex:
String regexPattern = "[0-9A-Za-z]+(st|nd|rd|th)" + " " + "floor";
I want to test it against:
String lineString = "8th floor, Prince's Building, 12 Chater Road";
so I do:
boolean isMatching = lineString.matches(regexPattern);
and it return false. Why?
I thought it had something to do with whitespaces in Java, so I removed the whitespace in the regexPattern variable so it reads
regexPattern = "[0-9A-Za-z]+(st|nd|rd|th)floor";
and matched it with a string without white space:
String lineString = "8thfloor,Prince'sBuilding,12ChaterRoad"
it still returns false. Why? Any help very much appreciated.
String.matches() only returns true if the entire string matches the pattern.
Try adding .* to the beginning and end of your regex.
Example:
String regex = ".*[0-9A-Za-z]+(st|nd|rd|th)" + " " + "floor.*";
This is not the best approach, however...
Here's a better alternative:
String input = "8th floor, Prince's Building, 12 Chater Road";
String regex = "[0-9A-Za-z]+(st|nd|rd|th)" + " " + "floor";
Pattern p = Pattern.compile(regex);
boolean isMatch = p.matcher(input).find();
If you want to extract the floor number, do this:
String input = "8th floor, Prince's Building, 12 Chater Road";
String regex = "([0-9A-Za-z])+(st|nd|rd|th)" + " " + "floor";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
if (m.find()) {
String num = m.group(1);
String suffix = m.group(2);
System.out.println("Welcome to the " + num + suffix + " floor!");
// prints 'Welcome to the 8th floor!'
}
Check out the Pattern API for a boatload of info about Java regular expressions.
Edited, per comments ...
The [0-9A-Za-z]+ part is greedily matching until the end of th.
Try [0-9] instead.

Categories

Resources