doufen1890 2013-10-08 18:16
浏览 366
已采纳

正则表达式匹配行首或一组字符

At first I thought this must be very easy, I am just overlooking something, but so far with my limited knowledge of regex I can't figure this out,

I have a regex like [some characters]MYNAME actual thing is:

    rx = rx + `[ ,\t,,\,,\(,=,@,\s]+(MYNAME)`

I want this regex to also detect a line that starts with MYNAME. So the question is, is there a way to add ^ inside [] with other things? or to OR the ^ with a [some characters]?

I can't make it work either with javascript or golang. If there are differences related to this matter, I am interested in the golang specific solutions.

  • 写回答

1条回答

  • douweng5420 2013-10-08 18:17
    关注

    You can use alternation. Also, there are some unnecessary characters in your character class:

    • I don't know what those commas are supposed to do? Did you intent them to act as separator? If yes, remove them.
    • Also, you don't need to escape ( in a character class.
    • Since you have added \s, you don't need to add \t and " " separately.

    So, your regex can be simplified to:

    "(?:[(=@\s]+|^)(MYNAME)"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法