简体   繁体   English

正则表达式匹配2个字符之间的文本

[英]Regex Match text between 2 Character

I have this kind of line : 我有这样的一句话:

#RT_POOL_CELL1_1 : RT_CELL_T :=  (NEXT     => 0,

#RT_IS_OK                   : BOOLEAN := TRUE;
#RT_RESULT_UPDATE_TM_INFO.TC_INFO : PUS.TYPES.RESULT_UPDATE_TM_INFO_T := PUS.TYPES.UPDATE_OK;

And I need to take RT_POOL_CELL1_1, RT_IS_OK and RT_RESULT_UPDATE_TM_INFO (without the dot .TC_INFO) in java with regex. 我需要在java中使用正则表达式获取RT_POOL_CELL1_1,RT_IS_OK和RT_RESULT_UPDATE_TM_INFO(不带点.TC_INFO)。

I tried to make that with this regex : \\s*[#][[:ascii:]]\\s*[:|.] 我尝试使用这个正则表达式:\\ s * [#] [[:ascii:]] \\ s * [:|。]

But it doen't work. 但它不起作用。 The thing to know is the word I need to take is always between # and :, they can contains space between word et sign. 要知道的是我需要采取的词总是介于#和:之间,它们可以包含word et sign之间的空格。 And if there is a ., I juste take the word before the dot. 如果有一个。,我会在点之前接受这个词。

Thank's for your help. 谢谢你的帮助。

You can use this regex based on lookarounds: 您可以根据外观使用此正则表达式:

(?<=#)[^:.]+?(?= *[:.])

Breakup of regex: 正则表达式分解:

(?<=#)     # makes sure there is preceding #
[^:.]+?    # match anything but : or . (non-greedy)
(?= *[:.]) # makes sure this is followed by 0 or more spaces and : or .

RegEx Demo RegEx演示

To match the word: 要匹配这个词:

(?<=#)[^: .]+(?=.*:)

See live demo . 查看现场演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM