Regex match and get string by pattern JAVA - java

I have here a pattern for email
Pattern pattern = Pattern.compile("^[A-Za-z0-9+_.-]+#(.+)$");
also i have a String contains messages
String message = "Han hannibal#domain.com im 20 years old, i just came here to say nothing..";
my problem is when matching pattern to string i get nothing. here what i do
Pattern pattern = Pattern.compile(pattern);
Matcher m = pattern.matcher(message);
if(m.find()) {
Log.d("TAG", m.group(1));
}else {
Log.d("TAG", "No email found on string");
}
i don't if my code was right but i just simply follow some article on fetching words on string using regex.

You need to remove the anchors from your regex.
Pattern pattern = Pattern.compile("\\b[A-Za-z0-9+_.-]+#(?:[^.\\s]+\\.)+\\w{2,4}\\b");
Example:
String message = "Han hannibal#domain.com im 20 years old, i just came here to say nothing..";
Pattern pattern = Pattern.compile("\\b[A-Za-z0-9+_.-]+#(?:[^.\\s]+\\.)+\\w{2,4}\\b");
Matcher m = pattern.matcher(message);
if(m.find())
{
System.out.println("TAG " + m.group());
}
else {
System.out.println("TAG " + "No email found on string");
}
Output:
TAG hannibal#domain.com

Related

Using regex in java to get a word from a string [duplicate]

This question already has answers here:
Difference between matches() and find() in Java Regex
(5 answers)
Closed 3 years ago.
I need to find the word "best" in a string using regex but it's throwing a "no match found" error. What am I doing wrong?
Pattern pattern = Pattern.compile("(best)");
String theString = "the best of";
Matcher matcher = pattern.matcher(theString);
matcher.matches();
String whatYouNeed = matcher.group(1);
Log.d(String.valueOf(LOG), whatYouNeed);
As per your requirement you have to find the string "best" in "the best of", so find() method suits your requirement instead of matches(). Please find the sample code snippet below:
Pattern pattern = Pattern.compile("best");
String theString = "the best of";
Matcher matcher = pattern.matcher(theString);
if(matcher.find()) {
System.out.println("found");
}else {
System.out.println("not found");
}
}
Use find() not matches!
public static void main(String[] args){
Pattern pattern = Pattern.compile("(best)");
String theString = "the best of";
Matcher matcher = pattern.matcher(theString);
if(matcher.find())
System.out.println("Hi!");
}
What I think you want is this.
String theString = "the best of";
String regex = "(best)";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(theString);
while (m.find()) {
String result = m.group(1);
System.out.println("found: " + result);
}
outputs:
found: best

java regular expression issue about capture group

public void test(){
String source = "hello<a>goodA</a>boys can goodB\"\n"
+ " + \"this can help";
Pattern pattern = Pattern.compile("<a[\\s+.*?>|>](.*?)</a>");
Matcher matcher = pattern.matcher(source);
while (matcher.find()){
System.out.println("laozhu:" + matcher.group(1));
}
}
Output:
laozhu:goodA
laozhu:href="www.baidu.com">goodB
Why the second match is not laozhu:goodB?
Try this Regex:
<a(?: .*?)?>(\w+)<\/a>
So your Pattern should look like this:
Pattern pattern = Pattern.compile("<a(?: .*?)?>(\\w+)<\\/a>");
It matches goodA and goodB.
For the detailed description, look here: Regex101.
Pattern pattern = Pattern.compile("<a.*?>(.*?)</a>");

Splitting a string java

I have a string in format:
<+923451234567>: Hi here is the text.
Now I want to get the mobile number(without any non-alphanumeric characters) ie 923451234567 in the start of the string in-between < > symbols, and also the text ie Hi here is the text.
Now I can place a hardcoded logic, which I am currently doing.
String stringReceivedInSms="<+923451234567>: Hi here is the text.";
String[] splitted = cpaMessage.getText().split(">: ", 2);
String mobileNumber=MyUtils.removeNonDigitCharacters(splitted[0]);
String text=splitted[1];
How can I neatly get the required strings from the string with regular expression? So that I don't have to change the code whenever the format of the string changes.
String stringReceivedInSms="<+923451234567>: Hi here is the text.";
Pattern pattern = Pattern.compile("<\\+?([0-9]+)>: (.*)");
Matcher matcher = pattern.matcher(stringReceivedInSms);
if(matcher.matches()) {
String phoneNumber = matcher.group(1);
String messageText = matcher.group(2);
}
Use a regex that matches the pattern - <\\+?(\\d+)>: (.*)
Use the Pattern and Matcher java classes to match the input string.
Pattern p = Pattern.compile("<\\+?(\\d+)>: (.*)");
Matcher m = p.matcher("<+923451234567>: Hi here is the text.");
if(m.matches())
{
System.out.println(m.group(1));
System.out.println(m.group(2));
}
You need to use regex, the following pattern will work:
^<\\+?(\\d++)>:\\s*+(.++)$
Here is how you would use it -
public static void main(String[] args) throws IOException {
final String s = "<+923451234567>: Hi here is the text.";
final Pattern pattern = Pattern.compile(""
+ "#start of line anchor\n"
+ "^\n"
+ "#literal <\n"
+ "<\n"
+ "#an optional +\n"
+ "\\+?\n"
+ "#match and grab at least one digit\n"
+ "(\\d++)\n"
+ "#literal >:\n"
+ ">:\n"
+ "#any amount of whitespace\n"
+ "\\s*+\n"
+ "#match and grap the rest of the string\n"
+ "(.++)\n"
+ "#end anchor\n"
+ "$", Pattern.COMMENTS);
final Matcher matcher = pattern.matcher(s);
if (matcher.matches()) {
System.out.println(matcher.group(1));
System.out.println(matcher.group(2));
}
}
I have added the Pattern.COMMENTS flag so the code will work with the comments embedded for future reference.
Output:
923451234567
Hi here is the text.
You can get your phone number by just doing :
stringReceivedInSms.substring(stringReceivedInSms.indexOf("<+") + 2, stringReceivedInSms.indexOf(">"))
So try this snippet:
public static void main(String[] args){
String stringReceivedInSms="<+923451234567>: Hi here is the text.";
System.out.println(stringReceivedInSms.substring(stringReceivedInSms.indexOf("<+") + 2, stringReceivedInSms.indexOf(">")));
}
You don't need to split your String.

difficulty using regex pattern

Please consider the following text :
String str=
"<div style=\"text-align:left;\">$#abc#$</div>$#pqr#$";
How can I get the abc and pqr.
I tried using below code
String tempStr =
"$#<div style=\"text-align:left;\">$#Order-CASNo#$</div>$#abc#$";
Pattern p = Pattern.compile("(?<=\\$#)(\\w*)(?=#\\$)");
Matcher m = p.matcher(tempStr);
List<String> tokens = new ArrayList<String>();
while (m.find()) {
System.out.println("Found a " + m.group() + ".");
but it give me just abc..i want answer as Order-CASNo and abc.
This is the regex:
EDIT:
\b(?<=\$\#)(.*?)(?=\#\$)\b
Regex Demo

Java how to setup regex for this string

So I'm trying to pull two strings via a matcher object from one string that is stored in my online databases.
Each string appears after s:64: and is in quotations
Example s:64:"stringhere"
I'm currently trying to get them as so but any regex that I've tried has failed,
Pattern p = Pattern.compile("I don't know what to put as the regex");
Matcher m = p.matcher(data);
So with that said, all I need is the regex that will return the two strings in the matcher so that m.group(1) is my first string and m.group(2) is my second string.
Try this regex:-
s:64:\"(.*?)\"
Code:
Pattern pattern = Pattern.compile("s:64:\"(.*?)\"");
Matcher matcher = pattern.matcher(YourStringVar);
// Check all occurance
int count = 0;
while (matcher.find() && count++ < 2) {
System.out.println("Group : " + matcher.group(1));
}
Here group(1) returns the each match.
OUTPUT:
Group : First Match
Group : Second Match
Refer LIVE DEMO
String data = "s:64:\"first string\" random stuff here s:64:\"second string\"";
Pattern p = Pattern.compile("s:64:\"([^\"]*)\".*s:64:\"([^\"]*)\"");
Matcher m = p.matcher(data);
if (m.find()) {
System.out.println("First string: '" + m.group(1) + "'");
System.out.println("Second string: '" + m.group(2) + "'");
}
prints:
First string: 'first string'
Second string: 'second string'
Regex you need should be compile("s:64:\"(.*?)\".*s:64:\"(.*?)\"")

Categories