This question already has answers here:
Using Regular Expressions to Extract a Value in Java
(13 answers)
Closed 6 years ago.
I have a string like this:
"text1 <text2> text3"
I want to grab only the text in <>. So I need to get text2. How can I do it?
You can do it like this:
String value = "text1 <text2> text3 <text4>";
Pattern pattern = Pattern.compile("<([^>]*)>");
Matcher matcher = pattern.matcher(value);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
Output:
text2
text4
Response Update:
Assuming that you know that you have only one value to extract, Bohemian proposes a simpler approach, he proposes to proceed as next:
String value = "text1 <text2> text3";
String target = value.replaceAll(".*<(.*)>.*", "$1");
System.out.println(target);
Output:
text2
I suggest you to split this string using " " as a separator - you will get 3 -elements array and the second one is what you are looking for
Related
This question already has answers here:
Can I replace groups in Java regex?
(6 answers)
Closed 1 year ago.
I'm trying to write code which finds all words that are in the following format
"some text":
So alphanumeric characters which are within " symbols and which end with the : symbol. My goal once I find these is to remove the " character and wrap the entire string in tag. So after the code has run, the above text would look like this,
<strong>some text:</strong>
So I believe the regex to find such text for wrapping is the following (formatted for Java),
(\".*?\"):{1}
And by using the following code, I should be able to iterate through all the matches.
Pattern pattern = Pattern.compile("(\".*?\"):{1}");
Matcher matcher = pattern.matcher(stringToSearch);
while(matcher.find()) {
String matchedGroup = matcher.group();
matchedGroup = matchedGroup.replaceAll("\"", "");
matchedGroup = "<strong>" + matchedGroup + "</strong>";
// now what?
}
So I probably went about that all wrong.
Now that I've wrapped the word I wanted in a strong tag, how do I "put it back" where it was?
Assuming you expect your double quotes to always be balanced, you may just use String#replaceAll here for a more terse solution:
String input = "Here is \"some text\": and also \"some other text\":";
String output = input.replaceAll("\"(.*?)\":", "<strong>$1:</strong>");
System.out.println(output);
This prints:
Here is <strong>some text:</strong> and also <strong>some other text:</strong>
This question already has answers here:
Android: Coloring part of a string using TextView.setText()?
(15 answers)
Closed 6 years ago.
Suppose I have the following string User 192.168.46.3 connected to this AP. I want to replace the IP address in there with <font color='red'>"+192.168.46.3+"</font> so that I can change it's color. What is the best way to achieve this?
If you do not care much about validating the IPs, a simple regex could do the trick.
Grab java.util.regex.Pattern and .Matcher and use something like
([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})
To replace the group you want to replace.
Like this:
final Pattern p = new Pattern("([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3})")
final Matcher m = p.match("User 192.168.46.3 connected to this AP")
final String s = m.replaceAll("<font color='red'>$1</font>")
This is the full code based on TimĀ“s answer:
String input = "User 192.168.46.3 connected to this AP";
String regex = "([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})";
String output = input.replaceAll(regex, "<font color='red'>$0</font>");
System.out.println(output);
You can use the Pattern.java and Matcher.java classes to detect ip addresses in your string.
Take a look at the Pattern.java docs, there is an exemple on how to use both of them correctly.
Then you can loop through your matcher results and apply a ForegroundColorSpan to each of them.
SpannableString spannable = new SpannableString(YOUR_STRING_CONTAINING_AN_IP_ADDRESS);
Pattern p = Pattern.compile(YOUR_REGEX);
Matcher m = p.matcher(YOUR_STRING_CONTAINING_AN_IP_ADDRESS);
while(m.find()) {
spannable.setSpan(new ForegroundColorSpan(COLOR_YOUR_WANT), m.start(), m.end()-1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
}
then you can use the spannable to set the text of a text view for example.
This question already has answers here:
Delete everything after part of a string
(10 answers)
Closed 6 years ago.
I am not an expert of regex. Suppose I have this string:
String str = "0,tcp,1.00,0.00,0.11,0.00,0.00,0.00,0.00,1.00,normal."
If I want to remove ,normal and replace it by dot so the string becomes like this:
String str = "0,tcp,1.00,0.00,0.11,0.00,0.00,0.00,0.00,1.00."
How can I do that in regex?
Thank you very much.
You can use a regular expression like ,\\w+\\.$ which matches any String ending in , a word and then a . and String.replaceAll(String, String) like
String str = "0,tcp,1.00,0.00,0.11,0.00,0.00,0.00,0.00,1.00,normal."
.replaceAll(",\\w+\\.$", "\\.");
System.out.println(str);
Output is (as requested)
0,tcp,1.00,0.00,0.11,0.00,0.00,0.00,0.00,1.00.
This question already has answers here:
Java Regex matching between curly braces
(5 answers)
Closed 6 years ago.
I am trying to extract value between { } using
"\\(\\{[^}]+\\}\\)"
regex in java. My input is
String text = "Hi this is {text to be extracted}."
I want output as
"text to be extracted"
but that regex isn't working.
Try this:
"\\{([^}]*)\\}"
Online Demo
Then $1 is containing text to be extracted.
The regexp seems malformed.
You need to match extra characters before and after the group, and you do not need to escape the parenthesis.
Also, you can use the named group to extract exactly the text you care about
Here is working code
String text = "Hi this is {text to be extracted}.";
Pattern p = Pattern.compile(".*\\{(?<t>[^}]+)\\}.*");
Matcher m = p.matcher(text);
if (m.matches()) {
System.out.println(m.group("t"));
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to replace special character and its next string with one string
I have the following String:
"Hello $employee.$currency and $bankbalance"
My task is replacing $ and the following String with another String.
I will get more Strings like this in run time and have to scan and identify anything that starts with $ and should replace with corresponding String.
At run time string with starts with $ should replaced with single string in all it occurences
I will suggest use regex "\$[^. \n\0$]+"
Find the first and last index of the pattern.
Pattern pattern = Pattern.compile("\$[^\. \\n$]+");
Matcher matcher = pattern.matcher(string)
if (matcher.find()) {
start = matcher.start()
end = matcher.end()
text = matcher.group()
}
and replace that part of the string.
String st = "Hello $employee.$currency and $bankbalance";
String pattern = "[$]\\w+";
String res = st.replaceAll(pattern,"mystring");
System.out.println(res);
Output = Hello mystring.mystring and mystring
For Java regex tutorial