使用Java模式和Matcher搜索包含标点符号(如前斜杠)的模式

使用Java模式和Matcher搜索包含标点符号(如前斜杠)的模式,java,regex,pattern-matching,Java,Regex,Pattern Matching,我有这样一个字符串:1/80%的商品售价为4400万美元或以上/90/55 所以基本上我会有一个带模式的字符串: “一些数字”“正斜杠”“一些带有任何标点符号的文本,包括正斜杠”“正斜杠”“一些数字”“正斜杠”“一些数字” 我不能只使用stringtokenizer并在正斜杠上标记,因为我的文本块中可能有正斜杠。我对在java中使用模式和匹配器非常陌生。关于我如何做到这一点有什么线索吗?或者可能有用的教程?提前谢谢 也许这可以作为正则表达式使用: ^\d+/.*/\d+/\d+$ 根据需要添加

我有这样一个字符串:
1/80%的商品售价为4400万美元或以上/90/55
所以基本上我会有一个带模式的字符串:

“一些数字”“正斜杠”“一些带有任何标点符号的文本,包括正斜杠”“正斜杠”“一些数字”“正斜杠”“一些数字”


我不能只使用stringtokenizer并在正斜杠上标记,因为我的文本块中可能有正斜杠。我对在java中使用模式和匹配器非常陌生。关于我如何做到这一点有什么线索吗?或者可能有用的教程?提前谢谢

也许这可以作为正则表达式使用:

^\d+/.*/\d+/\d+$

根据需要添加捕获组。只要文本不包含换行符,这应该可以工作。

也许这可以作为正则表达式工作:

^\d+/.*/\d+/\d+$

根据需要添加捕获组。只要文本不包含换行符,这就可以工作。

此正则表达式应该可以:

^(\d+)\/(.*?)\/(\d+)\/(\d+)$

演示:

此正则表达式应该可以:

^(\d+)\/(.*?)\/(\d+)\/(\d+)$
演示:

您正在寻找的#/#/#/#

以下是一些应该有效的代码:

    String toScan = "Did you know that 1/80 of all goods sold for $44 million or more/90/55? It's cool, because 1/5 of all people can type /1/2 like that.";
    String regexp = "[0-9]{1,}/[0-9]{1,}.{1,}?/[0-9]{1,}/[0-9]{1,}";
    Pattern pattern = Pattern.compile(regexp);
    Matcher m = pattern.matcher(toScan);
    while(m.find())
        System.out.println(m.group());
你在找什么#/#/#/#

以下是一些应该有效的代码:

    String toScan = "Did you know that 1/80 of all goods sold for $44 million or more/90/55? It's cool, because 1/5 of all people can type /1/2 like that.";
    String regexp = "[0-9]{1,}/[0-9]{1,}.{1,}?/[0-9]{1,}/[0-9]{1,}";
    Pattern pattern = Pattern.compile(regexp);
    Matcher m = pattern.matcher(toScan);
    while(m.find())
        System.out.println(m.group());
这里有一个简单的测试

import java.util.regex.*;

class RTest
{
public static void main(String[] args)
{
    String test1 = "1/80% of all goods sold for $44 million dollars or more/90/55";
    String test2 = "1/80% of all goods sold for $44 /million dollars or more/90/55";

    String patternStr = "(.*?)/(.*)/(.*?)/(.*?)$";
    Pattern pattern = Pattern.compile(patternStr);

    System.out.println("Test1...");
    // Test 1
    Matcher matcher = pattern.matcher(test1);
    boolean matchFound = matcher.find();

    if (matchFound)
    {
        for (int i = 0; i<=matcher.groupCount(); i++)
        {
            System.out.println(matcher.group(i));
        }
    }

    System.out.println("Test2...");
    // Test 2
    matcher = pattern.matcher(test2);
    matchFound = matcher.find();

    if (matchFound)
    {
        for (int i = 0; i<=matcher.groupCount(); i++)
        {
            System.out.println(matcher.group(i));
        }
    }       
}
import java.util.regex.*;
类RTest
{
公共静态void main(字符串[]args)
{
String test1=“以4400万美元或以上的价格售出的所有商品的1/80%/90/55”;
String test2=“1/80%的商品售价为4400万美元或以上/90/55”;
字符串模式str=“(.*?/(.*)/(.*?/(.*?)$”;
Pattern=Pattern.compile(patternStr);
System.out.println(“Test1…”);
//测试1
Matcher-Matcher=pattern.Matcher(test1);
布尔matchFound=matcher.find();
如果(找到匹配项)
{
对于(inti=0;i,这里有一个简单的测试

import java.util.regex.*;

class RTest
{
public static void main(String[] args)
{
    String test1 = "1/80% of all goods sold for $44 million dollars or more/90/55";
    String test2 = "1/80% of all goods sold for $44 /million dollars or more/90/55";

    String patternStr = "(.*?)/(.*)/(.*?)/(.*?)$";
    Pattern pattern = Pattern.compile(patternStr);

    System.out.println("Test1...");
    // Test 1
    Matcher matcher = pattern.matcher(test1);
    boolean matchFound = matcher.find();

    if (matchFound)
    {
        for (int i = 0; i<=matcher.groupCount(); i++)
        {
            System.out.println(matcher.group(i));
        }
    }

    System.out.println("Test2...");
    // Test 2
    matcher = pattern.matcher(test2);
    matchFound = matcher.find();

    if (matchFound)
    {
        for (int i = 0; i<=matcher.groupCount(); i++)
        {
            System.out.println(matcher.group(i));
        }
    }       
}
import java.util.regex.*;
类RTest
{
公共静态void main(字符串[]args)
{
String test1=“以4400万美元或以上的价格售出的所有商品的1/80%/90/55”;
String test2=“1/80%的商品售价为4400万美元或以上/90/55”;
字符串模式str=“(.*?/(.*)/(.*?/(.*?)$”;
Pattern=Pattern.compile(patternStr);
System.out.println(“Test1…”);
//测试1
Matcher-Matcher=pattern.Matcher(test1);
布尔matchFound=matcher.find();
如果(找到匹配项)
{

对于(int i=0;iDesign pattern?我将重新标记您的问题。因此分隔符是第一个斜杠和最后两个斜杠,任何其他斜杠都是文本的一部分?pattern和Mather是部分或正则表达式(regex)Java中的机制。您可以找到更多信息设计模式?我将重新标记您的问题。因此分隔符是第一个斜杠和最后两个斜杠,任何其他斜杠都是文本的一部分?模式和数学是Java中的部分或正则表达式(regex)机制。您可以找到更多信息