What regular expression not work in android - android

Hi i want a regular expression for a-z A-Z 0_9آ-ی( Persian)
String regex ="^(?:([a-z A-Z 0-9])||([آ-ی]))+$";
i do not want allow special characters like !##$%^&*()?><+- .
and my commllet code is :
public int check_first_last_name(String name){
String regex ="^(?:([a-zA-Z0-9])||([آ-ی]))+$";
Matcher matcher = Pattern.compile( regex ).matcher(name);
if (!matcher.find( )){
return 2;
}
return 1;
}

this regex accept every words in English ,Persian and Arabic if you want allow space change end of regex to "...0-9 "
if(name.matches("^([\\u0600-\\u06FF\\uFB8A\\u067E\\u0686\\u06AFa-zA-Z0-9])+$")){
//do what you want
}
please check in code you must have 2 backslash before every unicode not 4!
and if you want accept all tabs newline and space you can use this regex:
"^([\\u0600-\\u06FF\\uFB8A\\u067E\\u0686\\u06AFa-zA-Z0-9]+\\s?)+$"

Related

pattern regex ignoring back slash for quotes and single quote

I am using following pattern,
public static final String DEGREE_SIGN = "°";
public static final Pattern DMS = Pattern.compile("^\\s*(-?\\d{1,3})\\s*" + DEGREE_SIGN
+ "+\\s*(\\d{1,2})\\s*\'"
+ "+\\s*(\\d{1,2}\\.+?\\d{0,6})\\s*\"+$");
And trying with string as "38° 53' 23\""
Matcher m = DMS.matcher(latitudeDMSString);
if (m.find())
{
}
However its failing. In debug I can see regex as
java.util.regex.Matcher[pattern=^\s*(-?\d{1,3})\s*°+\s*(\d{1,2})\s*'+\s*(\d{1,2}\.+?\d{0,6})\s*"+$ region=0,11 lastmatch=]
It seems to be removing \ infront of ' and " How can I fix this
public static final Pattern DMS =
Pattern.compile("^\s*(-?\d{1,3})\s*°+\s*(\d{1,2})\s*\'+\s*(\d{1,2}\.?\d{1,4}?)\s*\"+$");
You need to change this part in your regex:
\\.+?
to
\\?
Basically you are trying to make dot optional and using \.+? just makes it a lazy match 1+ times.
RegEx Demo
In java following are the escape characters:-
\' Insert a single quote character in the text at this point.
\" Insert a double quote character in the text at this point.
\ Insert a backslash character in the text at this point.
Therefore, in order to use \' and \" in a string in java, we need to escape those characters by using \\\' and \\\" respectively.

Ignore starting blank space and special character only capitalize first alphabet from string

String name1 = " shashi";
Output: name1:" Shashi";
String name2 = "###shashi";
Output: name2: = "###Shashi";
String name3 = "##$&shashi";
Output: name3: = "##$&Shashi";
Note: Capitalize only first letter of alphabet, ignore space and special character.
Try this to remove Special Characters
public static String getOnlyStrings(String s) {
Pattern pattern = Pattern.compile("[^a-z A-Z]");
Matcher matcher = pattern.matcher(s);
String number = matcher.replaceAll("");
return number;
}
So your call should be
str = getOnlyStrings(str);
& then capitalized first letter using
str.replace(str.charAt(0),str.toUpperCase().charAt(0));
You may need to change the Pattern according to your needs, Current pattern only accepts Characters from a to z
Credits : Answer: How to remove special characters from a string?
str.replaceAll(" ","");
str.replace(str.charAt(0),str.toUpperCase().charAt(0));
hm..... if you want to ignore the special character, recommend to use ASCIICODE and charAt method.

Regular expression with hebrew

I have text like:
לשלום קוראים לי משהmy test is עלות 39.40, כל מיני data 1.1.2015 ויש גם data 123456 מידע
This text have Hebrew and English characters, I need to eliminate all except the 6 digit number (may be 5, this num: 123456).
Can you help me with regular expression for this?
Tried:
String patternS = "[אבגדהוזחטיכךלמםנןסעפףצץקרשתa-fA-F0-9]{5,10}.*";
Pattern pattern = Pattern.compile(patternString);
With no success
To match everything except the number use:
\d+(?:[^\d]\d+)+|[\p{L}\p{M}\p{Z}\p{P}\p{S}\p{C}]+
String resultString = subjectString.replaceAll("\\d+(?:[^\\d]\\d+)+|[\\p{L}\\p{M}\\p{Z}\\p{P}\\p{S}\\p{C}]+", "");
This will give you every 6 didgit combination in your string.
(\d{6,6})
We can't give you a more detailled regex since we do now know the pattern of those strings.
In case there is always the "data " prefix you can also use this to make the pattern more accurate:
data (\d{6,6})
Try something like this:
String patternS = "(\d{5,6})";
Pattern pattern = Pattern.compile(patternS);
Matcher m = pattern.matcher(yourText);
int number = Integer.parseInt(m.group(1));
where yourText is the Hebrew/English text you want to match.
This would work for this specific example.
String s = " לשלום קוראים לי מש my test is עלות 39.40, כל מיני data 1.1.2015 ויש גם data 123456 מידע1234";
System.out.println(s.replaceAll(".*\\b(\\d{5,6})\\b.*", "$1"));

How do I match the pattern in android

I am currently working on an android project. I would like to know if a string contains the '\r' or '\n' as the last character. How to do the pattern match?
Try this.
String string = "stackoverflow";
lastchar = string.substring(string.length() - 1);
It will give you the result "w".
try
String patterntomatch ="^[_A-Za-z0-9-]*(\r\n)$";
Pattern pattern=Pattern.compile(patterntomatch);
Matcher matcher=pattern.matcher(matchfromedittext);
boolean matcher.matches();
String last = your_string.substring(Math.max(your_string.length() - 2, 0));
//It will give you the last 2 characters of the string. If the string is null
//or has less than 2 characters, then it gives you the original string.
if(last.equals("\r") || last.equals("\n")){
//String's last two characters are either \n or \r. Now do something.
}

Regex in android for email,username & mobile number

I want to use regex in my android application to validate some field.
User Name :
1 Capital Letter[A-Z], 2 digit[0-9], 1 Special Character any and then followed by small character[a-z] and lenth would be 10 character max.
Email Address :
Must contain #google.com in last
Mobile :
Must be +91 and after that 10 digit.
How can I form my regex pattern for all three fields..?
Regx for emailid:
^[A-Za-z][A-Za-z0-9]*([._-]?[A-Za-z0-9]+)#[A-Za-z].[A-Za-z]{0,3}?.[A-Za-z]{0,2}$
accepts values as:
hdf4.j8k#bfv.djf
ds.sd#c25v.fdv
dv_sdv#fvv
vdf-f#jn.fdv
jfk#mbf.khb.in
n etc
Regex for Mobile No:
^[7-9][0-9]{9}$
works perfect for indian mobile numbers.
Regex for landline No:
^[0-9]{3,5}-[2-9]{1}[0-9]{5,7}$
for landline numbers in india with region code
eg: 022-58974658
You can find the regex you require for password, email and more , for instance
For Username :
^[a-z0-9_-]{3,15}$
^ # Start of the line
[a-z0-9_-] # Match characters and symbols in the list, a-z, 0-9
, underscore , hyphen
{3,15} # Length at least 3 characters and maximum length of 15
$ # End of the line
at : http://www.mkyong.com/regular-expressions/10-java-regular-expression-examples-you-should-know/
Please see below link for Email Validation and you can modify some part of the code for username validation and phone number validation.
how-to-check-edittexts-text-is-email-address-or-not
public static boolean isEmailValid(String email) {
boolean isValid = false;
String expression = "^[\\w\\.-]+#([\\w\\-]+\\.)+[A-Z]{2,4}$";
CharSequence inputStr = email;
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputStr);
if (matcher.matches()) {
isValid = true;
}
return isValid;
}
Mobile :
Must be +91 and after that 10 digit.
^[7-9][0-9]{9}$ is useful for only mobile numbers. But for country code we should have to use regex like ^[+(00)][0-9]{6,14}$ ..
something like
1)
String phoneNumber = "+919900990000"
if(phoneNumber.matches("^[+(00)][0-9]{6,14}$")){
//True
2)
String phoneNumber = "9900990000"
if(phoneNumber.matches("^[+(00)][0-9]{6,14}$")){
//False
The first is true because it has the country code with it, But the second one is false because it has not any country code attached with it.
You can use Patterns class for validating factors such as email,mobile no etc.
Here's how:
public static boolean isValidEmail(CharSequence target) {
return (!TextUtils.isEmpty(target) && Patterns.EMAIL_ADDRESS.matcher(target).matches());
}
public static boolean isValidMobile(CharSequence target) {
return (!TextUtils.isEmpty(target) && Patterns.PHONE.matcher(target).matches());
}
Hope it'll help you.

Categories

Resources