【问题标题】:regex: Matching multiline comments? [duplicate]正则表达式:匹配多行注释? [复制]
【发布时间】:2017-11-27 01:20:54
【问题描述】:

我正在尝试在源代码中匹配多行 cmets,如下所示:

    /**
     * Loads an {@link Ext.data.Model} into this form (internally just calls {@link Ext.form.Basic#loadRecord})
     * See also {@link Ext.form.Basic#trackResetOnLoad trackResetOnLoad}. The fields in the model are mapped to 
     * fields in the form by matching either the {@link Ext.form.field.Base#name} or {@link Ext.Component#itemId}.  
     * @param {Ext.data.Model} record The record to load
     * @return {Ext.form.Basic} The Ext.form.Basic attached to this FormPanel
     */
    loadRecord: function(record) {
        return this.getForm().loadRecord(record);
    },

/**
 * Convenience function for fetching the current value of each field in the form. This is the same as calling
 * {@link Ext.form.Basic#getValues this.getForm().getValues()}.
 *
 * @inheritdoc Ext.form.Basic#getValues
 */
getValues: function(asString, dirtyOnly, includeEmptyText, useDataValues) {
    return this.getForm().getValues(asString, dirtyOnly, includeEmptyText, 
  useDataValues);
},

它应该只返回注释部分(以 /** 开头并以 */ 结尾)。到目前为止,我有以下内容:

^(\/\*\*)(.|\n)*$(\*\/)

【问题讨论】:

  • 永远不要使用(.|\n)+?,这是一个性能杀手。在链接线程中使用我的方法以避免在多行 cmets 很长的情况下使用您的模式出现 stackoverflow 异常。

标签: java regex


【解决方案1】:

在这里,试试这个:

(\/\*\*)(.|\n)+?(\*\/)

这应该完全符合您的要求。第一个捕获组只匹配/**。第二组匹配任何其他字符,+ 匹配任意数量的该标记。 ? 使搜索变得懒惰,只匹配到下一次出现,因此我们不匹配从第一条评论的开头到第二条评论的结尾以及介于两者之间的所有内容。

【讨论】:

    猜你喜欢
    • 2013-04-20
    • 1970-01-01
    • 2012-10-26
    • 2011-10-29
    • 2010-11-14
    • 2014-04-18
    • 2012-10-12
    • 2018-01-07
    • 2021-09-03
    相关资源
    最近更新 更多