Android Matcher and Pattern cut up from the link - android

I have a string that contains a link.
ex. of the string:
We love to eat choco, eat one up at http://t.co/9BDZvcx59d.
So If I display this string as it is, it would be the same. But if I use matcher and pattern to detect the link and color it, it will cut it up.
It'll be: Bold is green color.
We love to eat choco, eat one up at http://t.co.
Pattern urlPattern = Patterns.WEB_URL;
Matcher m = urlPattern.matcher(sb.toString());
sb = new StringBuffer(sb.length());
while (m.find()) {
m.appendReplacement(sb, "<font color=\"#006600\">" + m.group(1) + "</font>");
}
m.appendTail(sb);
I also tried using
Pattern linkPattern = Pattern.compile("(http[A-Za-z0-9_-+)");
But couldn't insert : or // in the [].

You can try this for your second method.
Pattern linkPattern = Pattern.compile("(http://t.co/[A-Za-z0-9_-]+)");
It will compile after the t.co/ so it won't face symbols or anything but letter and numbers.

Try to add following attributes to your TextView in xml
android:autoLink="web"
android:textColorLink="your-color-code"

Related

Replacing a word ignoring spaces and cases from a string in Android

What I'm trying to do:
if a String is
String a = "Love is rare"; Or
String a = "l o v e is rare"; Or
String a = "LO VE is rare";
Then,
I want the word "love" to be replaced by "hate" ignoring the space and case
My code:
a=a.toLowerCase().replace("love","hate").replace("\\s+","");
But the problem is that it removes all the spaces and change all the words to lower case. I just want to check if there is a word love by ignoring all the spaces and cases and if the word is there then replace it with some other word.
Please help!
Thank you.
Try it like this using an optional whitespace ? and the case insensitive flag. If you want to match zero or more whitespaces, you could use a whitespace and *
l ?o ?v ?e
String regex = "l ?o ?v ?e";
String[] lines = {"Love is rare", "l o v e is rare", "LO VE is rare"};
String subst = "hate";
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
for (String line: lines) {
Matcher matcher = pattern.matcher(line);
String result = matcher.replaceAll(subst);
System.out.println(result);
}
That would give you:
hate is rare
hate is rare
hate is rare
Demo Java
Thanks for suggesting the term regex, Studied thoroughly and come up with this one line function which will replace "love" by "hate" ignoring the spaces and cases:
String a = "Lo ve is rare.";
Code:
a = a.replaceAll("(?i)l(\\s*)o(\\s*)v(\\s*)e","hate");
TextView.setText(a);
This would give:
hate is rare.

how to convert plain text into html text in android?

I am uploading the text to server, i just want to upload those string in html format
example
input:
Do you know the relation between two eyes...???
They never see each other... BUT
They blink together.
They move together.
They cry together.
They see together.
They sleep together.
They share a very deep bonded relationship...
However, when they see a pretty woman, one will blink and another will not...
sendtext = adding_textjoke.getText().toString();
//String htmlString = Html.toHtml(sendtext);
String str = "(?i)\\b((?:https?://|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:\'\".,<>?«»“”‘’]))";
Pattern patt = Pattern.compile(str);
Matcher matcher = patt.matcher(sendtext);
sendtext = matcher.replaceAll("$1");
System.out.println(sendtext);
Log.e("sendtext", sendtext);
new AddJokesTask().execute(sendtext);
How to do this in android?
You can do it like this
SpannableString contentText = (SpannableString) contentView.getText();
String htmlEncodedString = Html.toHtml(contentText)
SpannableStringBuilder text = (SpannableStringBuilder) contentView.getText();
String htmlEncodedString = Html.toHtml(text);

how to get text with using substring

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).

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"));

Android and regex to read substring

I'm trying to use regex in an Android application to read a substring. I'm using Pattern and Matcher, but I can't figure out how to do it.
My input string is: javascript:submitForm(document.voip_call_log,30,0,'xxxxx','',0,'');
where xxxxx is a variable number of digits.
How can I read 'xxxxx' using Pattern and Matcher?
I'm not an expert in RegEx, but this one should work for you:
String input = "javascript:submitForm(document.voip_call_log,30,0,'5555','',0,'');";
Pattern pattern = Pattern.compile(",'(\\d*)',");
Matcher matcher = pattern.matcher(input);
matcher.find();
String out = matcher.group(1);
System.out.println(out);
Proof http://ideone.com/WI6VB1
If you will have always that format, it might be easier to use the split(",") method and access the value you like.
Alternatively, you could try something like so:
String str = ...;
Pattern p = Pattern.compile("(\\d{3,})");
Matcher m = p.matcher(str);
while(m.find())
{
System.out.println(m.group(1));
}
Try this one:
String s = "javascript:submitForm(document.voip_call_log,30,0,'1999','',0,'');";
Pattern pattern = Pattern.compile("javascript:submitForm\\([^,]+,\\d*,\\d*,'(\\d+)','\\d*'");
Matcher m = pattern.matcher(s);
if(m.find( )) {
System.out.println(m.group(1));
}
This is following the pattern of your input string from the question description. You can find the explanation of your regex from this link. Just input your regex with single slash \ instead of double \\

Categories

Resources