使用正则表达式找出方括号内的整个字符串并匹配一个单词

时间:2018-08-30 13:46:44

标签: regex apex

我想找出方括号内的整个字符串和一个单词。

示例字符串= [This is my first Question];

我要搜索[This ..

如果字符串包含[This,我的预期输出应该是括号内的整个字符串。

有人可以使用正则表达式来帮助吗?任何帮助,将不胜感激。 我的代码如下:

string mainStr = 'wrapper s = [This is my first Question]'; 
Pattern pattr = Pattern.compile('\[This[^]]+\]'); 
Matcher mat = pattr .matcher(mainStr ); 
system.debug('mat is -----'+mat.matches());
system.debug('m is -----'+mat.find());
string n = null; 
if(mat.matches()) { n = mat.group(); system.debug('m is ----'+n); } 

1 个答案:

答案 0 :(得分:0)

如果使用regEx (\[(\w+)[^]]+\])

你得到

  • 在$ 1中,[]和
  • 之间的字符串
  • 在$ 2中,第一个单词的含义。

在下面,您将看到在文本编辑器(phpStorm)中使用的一个演示:

enter image description here