Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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 - Fatal编程技术网

Java 字符串操作-富文本编辑器

Java 字符串操作-富文本编辑器,java,regex,Java,Regex,我有一个要求。我有一个字符串,它有一个值,例如: <p>We are pleased <a href="http://www.anc.com/content/cy-tech/global/en/cq5-reference-materials.s_cid_123.html">to present the new product type</a>. This new product type is the best thing since sliced bread

我有一个要求。我有一个字符串,它有一个值,例如:

<p>We are pleased <a href="http://www.anc.com/content/cy-tech/global/en/cq5-reference-materials.s_cid_123.html">to present the new product type</a>. This new product type is the best thing since sliced bread. We are pleased to present the new product type. This new product <a href="mailto:abc@gmail.com">type is the best</a> thing since sliced bread.</p>
描述 该正则表达式将:

  • 在锚定标记中查找href属性
  • 要求href具有
    http://abc.com
    。它还允许
    https
    www.abc.com
    处于各自的位置
  • 如果字符串包含
    ,则该字符串也将被捕获并放入组捕获3中

  • 。如果您发现这是过度的,或者它与嵌套的锚标记冲突,那么只需删除
    [^它与Java有什么关系?我需要Java中的代码。至少你应该放弃正则表达式。用正则表达式解析标记语言不是一个很好的主意。还有为什么Javascript?上面的代码块没有按预期工作…@user1661908添加有问题的两种方法并删除它们的注释。这将使它更易于阅读,并且所有用户都将成为一个用户能够阅读有问题的评论(不是每个人都在阅读评论).1对于免责声明,如果我可以问的话,你是如何创建状态图的?@wazy,我认为该评论应该真正应用于这个问题,而不是一个可能的解决方案,因为它也有一个免责声明支持你链接背后的想法。@qqilihq,我使用的是debuggex.com。虽然它不支持lookbehinds或原子组,但对你来说仍然很方便了解表达式流。还有regexper.com。它们也做得很好,但在你打字时不是实时的。
    
    String tmpStr = "href=\"http://www.abc.com\">design";
    
    StringBuffer tmpStrBuff = new StringBuffer();
    String[] tmpStrSpt = tmpStr.split(">");
    if (tmpStrSpt[0].contains("abc.com")) {
        String[] tmpStrSpt1 = tmpStrSpt[0].split("\"");
        tmpStrBuff.append(tmpStrSpt1[0]);
        if (tmpStrSpt1[1].contains("?")) {
            tmpStrBuff.append("\"" + tmpStrSpt1[1] + "&s_cid=abcd_xyz\">");
        } else {
            tmpStrBuff.append("\"" + tmpStrSpt1[1] + "?s_cid=abcd_xyz\">");
        }
        tmpStrBuff.append(tmpStrSpt[1]);
        tmpStrBuff.append("</a>");
        System.out.println(" <p>tmpStr1:::: " + tmpStrBuff.toString() + "</p>");
    }
    
    String[] tmpTxtArr = text.split("\\s+");
    StringBuffer tmpStrBuff = new StringBuffer();
    for (String tmpTxt : tmpTxtArr) {
        descTxt += (tmpTxt.contains("abc.com") && !tmpTxt.contains("?")) ? tmpTxt
                .replace("\">", "?s_cid=" + trackingCode + "\">" + " ")
                : tmpTxt + " ";
    }
    
    <p>Some <a href="http://www.abc.com/content/cy-tech/global/en/cq5-reference-materials.s_cid_123.html">text</a>. I like kittens <a href="mailto:abc@gmail.com">email us</a>Dogs are nice.</p><a href="http://www.abc.com/content/cy-tech/global/en/cq5-reference-materials.s_cid_123.html?attribute=value">remember to vote</a>
    
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;
    class Module1{
      public static void main(String[] asd){
      String sourcestring = "source string to match with pattern";
      Pattern re = Pattern.compile("<a\\b[^<]*\\bhref=(['\"])(https?:\\/\\/(?:www[.])?abc[.]com[^\"'?]*?([?]?)[^\"'?]*?)\\1[^<]*<\\/a>",Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
      Matcher m = re.matcher(sourcestring);
      int mIdx = 0;
        while (m.find()){
          for( int groupIdx = 0; groupIdx < m.groupCount()+1; groupIdx++ ){
            System.out.println( "[" + mIdx + "][" + groupIdx + "] = " + m.group(groupIdx));
          }
          mIdx++;
        }
      }
    }
    
    $matches Array:
    (
        [0] => Array
            (
                [0] => <a href="http://www.abc.com/content/cy-tech/global/en/cq5-reference-materials.s_cid_123.html">text</a>
                [1] => <a href="http://www.abc.com/content/cy-tech/global/en/cq5-reference-materials.s_cid_123.html?attribute=value">remember to vote</a>
            )
    
        [1] => Array
            (
                [0] => "
                [1] => "
            )
    
        [2] => Array
            (
                [0] => http://www.abc.com/content/cy-tech/global/en/cq5-reference-materials.s_cid_123.html
                [1] => http://www.abc.com/content/cy-tech/global/en/cq5-reference-materials.s_cid_123.html?attribute=value
            )
    
        [3] => Array
            (
                [0] => 
                [1] => ?
            )
    
    )