I am trying to capture just 1 word before the equals sign but what is happening is that it is capturing everything before the equal sign. Here is my code:
public static void main(String[] args)
{
String text = "This is = an equal sign";
Pattern p = Pattern.compile(" (.*?)=");
Matcher matcher = p.matcher(text);
if (matcher.find())
{
System.out.println("Match: " + matcher.group(0));
}
}
This is the output that I got:
Match: This is =
Try below code:
class Ideone
{
public static void main(String[] args)
{
String text = "This is = an equal sign";
Pattern p = Pattern.compile("(\\w+)\\s?=");
Matcher matcher = p.matcher(text);
if (matcher.find())
{
System.out.println("Match: " + matcher.group(1));
}
}
}
Output : Match: is
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.