2 u013179958 u013179958 于 2017.01.09 08:49 提问

正则表达式如何实现,匹配以*开头的一行?

匹配下面代码中以*开头的行

 public class TransferForm extends BaseObject{
    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column GC_TRANSFER_FORM.FORM_ID
     *
     * @mbggenerated Wed Jan 04 13:58:02 CST 2017
     */
    private String formId;

}

3个回答

showbo
showbo   Ds   Rxr 2017.01.09 09:12
已采纳

/^\s**[^\n]+$/gm,记得加m选项,多行匹配


<textarea id="ta">
    public class TransferForm extends BaseObject{
    /**
    * This field was generated by MyBatis Generator.
    * This field corresponds to the database column GC_TRANSFER_FORM.FORM_ID
    *
    * @mbggenerated Wed Jan 04 13:58:02 CST 2017
    */
    private String formId;

    }
</textarea>
<script>
    var s = ta.value, mc = s.match(/^\s*\*[^\n]+$/gm);
    console.log(mc)
</script>
tang_cheng
tang_cheng   2017.01.09 08:57

正则表达式为:^\s**

q1010174887
q1010174887 ^\*.*
11 个月之前 回复
u013179958
u013179958 回复tang_cheng: 无法匹配
11 个月之前 回复
tang_cheng
tang_cheng 正则表达式为:^\s*\* (全角的\换成半角的)
11 个月之前 回复
jane_pop
jane_pop   2017.01.09 09:18

[test@localhost shell]$ cat test.txt
public class TransferForm extends BaseObject{
/**

  • This field was generated by MyBatis Generator.
  • This field corresponds to the database column GC_TRANSFER_FORM.FORM_ID *
  • @mbggenerated Wed Jan 04 13:58:02 CST 2017 */ private String formId;

}
[test@localhost shell]$ cat test.txt |sed 's/^[[:space:]]*//g'|grep '^*'

  • This field was generated by MyBatis Generator.
  • This field corresponds to the database column GC_TRANSFER_FORM.FORM_ID *
  • @mbggenerated Wed Jan 04 13:58:02 CST 2017 */_

cat test.txt |sed 's/^[[:space:]]*//g'|grep '^*'
先将行首的空格都去掉,之后筛选出以*开头的行。
不知道你是不是这意思~~~

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!