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.
}
Related
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.
mates...
I am having troubles to pass through a regex accepting accents in android... All the things we have try like for java is not working properly, and android don't want our accented vocals ..
I have the following regex:
Pattern pattern = Pattern.compile("[a-zA-ZñÑáéíóúÁÉÍÓÚ]+");
Any tip of how to include ñ and accents vocals in android?
Thanks very much in advance...
Here is our validation function:
public static boolean validarNombres(String nameToValidate){
byte step = 1;
byte minWords = 2;
byte maxWords = 5;
boolean validName = false;
String[] aux;
Matcher matcher = null;
Pattern pattern = Pattern.compile("[\\p{L}\\p{M}]+");
aux = nameToValidate.split(" ");
//PASO 2: check that the name has from 2 to 5 words
if(aux.length >= minWords && aux.length <= maxWords){
step++;
matcher = pattern.matcher(nameToValidate);
}
//PASO 3: check that the name matches out regex
if(step==2 && matcher.matches()){
validName = true;
}
return validName;
}
EDIT: Think found the mistake... We are not including the blank space bettwen the first and second name ... It works fine when we check just a word, but not for the full name... now....
Whats the code to include a blank space on our regex?, please
Thanks very much
To validate a string consisting of 2 to 5 words separated with whitespace(s), you may use
public static boolean validarNombres(String nameToValidate) {
return nameToValidate.matches("[\\p{L}\\p{M}]+(?:\\s[\\p{L}\\p{M}]+){1,4}");
}
The regex is anchored by default when used with the .matches() method, no need adding ^ and $.
Pattern details:
[\\p{L}\\p{M}]+ - 1 or more letters or/and diacritics
(?:\\s[\\p{L}\\p{M}]+){1,4} - 1 to 4 (so, 2 to 5 in total) sequences of:
\\s - a single whitespace
[\\p{L}\\p{M}]+ - 1 or more letters or/and diacritics
See the regex demo.
Basically, what I am trying to do is add double quote to the heads and tails of the numbers
String a = 1;
String b = 2;
String c = 3;
to
String a = "1";
String b = "2";
String c = "3";
So, I use [1-9] to find all numbers. Then, all of a sudden, it comes to me that I don't know how to get the values which regex found, like don't know what to set between double quotes.
Hence, I am wondering if it's possible.
You should use \d+ instead of [1-9] or at the very least [0-9]+ to include the 0
The reason why you need the + is because your regex would not find 10 or any digits that has more than 1 digit. You can reference the groups that you have found by using $1 (first group) $2 (second group) and so on. So you could do "$1" as your substitution and (\d+) as your search although you might want to use a better regex ie:
=\s*(\d)+;
replace to
= "$1";
See https://regex101.com/r/SaT6nK/1
I try to get only this part "9916-4203" in "Region Code:9916-4203 " in android. How can I do this?
I tried below code, I used substring method but it doesn't work:
firstNumber = Integer.parseInt(message.substring(11, 19));
If you know that string contains "Region Code:" couldn't you do a replace?
message = message.replace("Region Code:", "");
Assumed that you have only one phone number in your String, the following will remove any non-digit characters and parse the resulting number:
public static int getNumber(String num){
String tmp = "";
for(int i=0;i<num.length();i++){
if(Character.isDigit(num.charAt(i)))
tmp += num.charAt(i);
}
return Integer.parseInt(tmp);
}
Output in your case: 99164203
And as already mentioned, you won't be able to parse any String to Integer in case there are any non-digit characters
Im going to guess that what you want to extract is the full region code text minus the title. So maybe using regex would be a good simple fit for you?
String myString = "Region Code:9916-4203";
String match = "";
String pattern = "\:(.*)";
Pattern regEx = Pattern.compile(pattern);
Matcher m = regEx.matcher(myString);
// Find instance of pattern matches
Matcher m = regEx.matcher(myString);
if (m.find()) {
match = m.group(0);
}
Variable match will contain "9916-4203"
This should work for you.
Java code sourced from http://android-elements.blogspot.in/2011/04/regular-expressions-in-android.html
In Java the substring() method works with the first parameter being inclusive and the second parameter being exclusive. Meaning "Hello".substring(0, 2); will result in the string He.
In addition to excluding the parsing of something that isn't a number like #Opiatefuchs mentioned, your substring method should instead be message.substring(12, 21).
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"));