Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中查找字符串中的标记图像语法_Java_Regex_Markdown - Fatal编程技术网

在Java中查找字符串中的标记图像语法

在Java中查找字符串中的标记图像语法,java,regex,markdown,Java,Regex,Markdown,我有一个Java的长文本,其中至少包含一个标记图像语法。如果存在Nmarkdown图像语法,我需要将字符串拆分为N+1子字符串,并将它们存储在字符串数组中,调用text。例如,我有以下文本 Hello world! ![Alt text](/1/2/3.jpg) Hello Stack Overflow! 然后你好,世界\n将存储在位置0和\n堆栈溢出将存储在位置1。对于我的问题,我们可以假设 Alt文本部分仅包含字符A-Z、A-Z和空格 URL部分仅包含数字0-9和斜杠/。其扩展名将仅为.

我有一个Java的长文本,其中至少包含一个标记图像语法。如果存在
N
markdown图像语法,我需要将字符串拆分为
N+1
子字符串,并将它们存储在字符串数组中,调用
text
。例如,我有以下文本

Hello world!
![Alt text](/1/2/3.jpg)
Hello Stack Overflow!
然后
你好,世界\n
将存储在位置0和
\n堆栈溢出将存储在位置1。对于我的问题,我们可以假设

  • Alt文本部分仅包含字符A-Z、A-Z和空格
  • URL部分仅包含数字0-9和斜杠
    /
    。其扩展名将仅为
    .jpg
    。其他扩展将不存在
我的问题是如何分割文本?我们需要java正则表达式吗,比如
*![*](*.jpg)

试试这个(准备复制粘贴):

“!\\[[^\]]+\]\\([^)]+\\”

有关如何获取匹配项的信息,请参阅

“未污染”版本:
\[[^\]+\]\([^]+\)

解释
  • 按字面意思
  • \[
    转义的
    [
  • [^\]]+
    尽可能多的不
    ]
    s
  • \]\(
    转义
    ](
  • [^]+
    尽可能多的不
    s
  • \)
    转义
  • 这是我的方式

    public class Test {
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
         List<String> allMatches = new ArrayList<String>();
         String str = "}```![imageName](/sword?SwordControllerName=KMFileDownloadController&id=c60b6c5a8d9b46baa1dc266910db462d \"imageName\")#### JSON data";
         Matcher m = Pattern.compile("\\[.*\\]\\((.*)\\)").matcher(str);
         while (m.find()) {
             allMatches.add(m.group(1).split(" ")[0]);
         }
         //print "/sword?SwordControllerName=KMFileDownloadController&id=c60b6c5a8d9b46baa1dc266910db462d"
         for(String s:allMatches){
             System.out.println(s);
         }
      }
    }
    
    公共类测试{
    公共静态void main(字符串[]args){
    //TODO自动生成的方法存根
    List allMatches=new ArrayList();
    String str=“}```![imageName](/sword?SwordControllerName=KMFileDownloadController&id=c60b6c5a8d9b46baa1dc266910db462d \“imageName \”)JSON数据;
    Matcher m=Pattern.compile(“\\[\\\\]\\(.*\\)”).Matcher(str);
    while(m.find()){
    allMatches.add(m.group(1).split(“”[0]);
    }
    //打印“/sword?SwordControllerName=KMFileDownloadController&id=c60b6c5a8d9b46baa1dc266910db462d”
    for(字符串s:allMatches){
    系统输出打印项次;
    }
    }
    }
    


    这样,Alt文本可以保持为空——尽管它毫无意义

    regex,当然——为什么不呢。你的正则表达式符号与标准符号不同吗?不,我的正则表达式符号应该与标准符号相同。如果有错误,那是我的错。(我对正则表达式知之甚少)@MincongHuang补充道!我解释了“未被玷污”的版本。很好的解释。我从中学到了很多,谢谢@lauretally,转义内容(降价)对我很有用。我可以获取它们并将它们放入其他字符串数组中吗?
    !\[[^\]]*?\]\([^)]+\)