在模式匹配正则表达式中使用“或”

疯狂的

我正在使用正则表达式查找匹配的模式。但是不知何故我找不到所有的职业。

我需要匹配模式的输入文件(请注意,这是一个示例文件,实际上只有3次出现-它具有多个Occrences):

aaa-233- hi, how are you? 
aaa-234- 6(-8989) 
aaa-235- 123
end

所以,我希望我的输出是

 hi, how are you? 
 6(-8988) 
 123

我的正则表达式是

aaa\\-[A-Za-z0-9,->#]\\-(.+?)(aaa) 

伪代码

Output= matcher.group(2);

如何使逻辑从aaa开始读取并结束遇到aaa或结束的逻辑。

Ryszard捷克

采用

(?sm)^aaa-[^-]+-.*?(?=\naaa|\nend|\z)

证明

说明

                         EXPLANATION
--------------------------------------------------------------------------------
  (?ms)                    set flags for this block (with ^ and $
                           matching start and end of line) (with .
                           matching \n) (case-sensitive) (matching
                           whitespace and # normally)
--------------------------------------------------------------------------------
  ^                        the beginning of a "line"
--------------------------------------------------------------------------------
  aaa-                     'aaa-'
--------------------------------------------------------------------------------
  [^-]+                    any character except: '-' (1 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  -                        '-'
--------------------------------------------------------------------------------
  .*?                      any character (0 or more times (matching
                           the least amount possible))
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    \n                       '\n' (newline)
--------------------------------------------------------------------------------
    aaa                      'aaa'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    \n                       '\n' (newline)
--------------------------------------------------------------------------------
    end                      'end'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    \z                       the end of the string
--------------------------------------------------------------------------------
  )                        end of look-ahead

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章