java 模式匹配器语法,选择性地无法识别字符串
java pattern matcher syntax, selectively incapable of string recognition
我正在使用 java 模式匹配器来梳理出 'XXX'('XXX','XXX') 形式的字符串。我不仅想要文本,即 XXX
这是我目前使用的:
Pattern p = Pattern.compile("'(.*?)'\('(.*?)','(.*?)'\)\.");
它能匹配这个:
'prevents'('scurvy','vitamin C').
'contains'('vitamin C','orange').
'contains'('vitamin C','sauerkraut').
'isa'('fruit','orange').
'improves'('health','fruit').
但是无法识别这一点,尽管它们的格式相同。
'take place in'('the grand hall of the hong kong convention', 'the ceremony').
'attend by'('some # guests', 'the grand hall of the hong kong convention').
'seat on'('the central dais', 'principal representatives of both countries').
'be'('mr jiang', 'representing china').
'be'('hrh', 'britain').
'be more than'('# distinguished guests', 'the principal representatives').
'end with'('the playing of the british national anthem', 'hong kong').
'follow at'('the stroke of midnight', 'this').
'take part in'('the ceremony', 'both countries').
'start at about'('# pm', 'the ceremony').
'end about'('# am', 'the ceremony').
'lower'('the british hong kong flag', '# royal hong kong police officers').
'raise'('the sar flag', 'another #').
'leave for'('the royal yacht britannia', 'the #').
'hold by'('the chinese and british governments', 'the handover of hong kong').
'rise over'('this land', 'the regional flag of the hong kong special administrative region of the people \'s republic of china').
'cast eye on'('hong kong', 'the world').
'hold on'('schedule', 'the # governments').
'be festival for'('the chinese nation', 'this').
'go in'('the annals of history', 'july # , #').
'become master of'('this chinese land', 'the hong kong compatriots').
'enter era of'('development', 'hong kong').
'remember'('mr deng xiaoping', 'history').
'be along'('the course', 'it').
'resolve'('the hong kong question', 'we').
这是什么原因?
有没有网站可以演示我的正则表达式应用于 java 模式匹配器?喜欢 regexr.com
或者一些简单易懂的文档也很好,我的 google 搜索结果非常零散且不连贯。
因为所有的逗号后面都有一个space。
所以我建议你使用\s*
(匹配零个或多个spaces)或\s?
(匹配可选的 space),
Pattern p = Pattern.compile("'(.*?)'\('(.*?)',\s*'(.*?)'\)\.");
示例:
'prevents'('scurvy','vitamin C').
^
| - no space
但是
'take place in'('the grand hall of the hong kong convention', 'the ceremony').
^
|- space
我正在使用 java 模式匹配器来梳理出 'XXX'('XXX','XXX') 形式的字符串。我不仅想要文本,即 XXX
这是我目前使用的:
Pattern p = Pattern.compile("'(.*?)'\('(.*?)','(.*?)'\)\.");
它能匹配这个:
'prevents'('scurvy','vitamin C').
'contains'('vitamin C','orange').
'contains'('vitamin C','sauerkraut').
'isa'('fruit','orange').
'improves'('health','fruit').
但是无法识别这一点,尽管它们的格式相同。
'take place in'('the grand hall of the hong kong convention', 'the ceremony').
'attend by'('some # guests', 'the grand hall of the hong kong convention').
'seat on'('the central dais', 'principal representatives of both countries').
'be'('mr jiang', 'representing china').
'be'('hrh', 'britain').
'be more than'('# distinguished guests', 'the principal representatives').
'end with'('the playing of the british national anthem', 'hong kong').
'follow at'('the stroke of midnight', 'this').
'take part in'('the ceremony', 'both countries').
'start at about'('# pm', 'the ceremony').
'end about'('# am', 'the ceremony').
'lower'('the british hong kong flag', '# royal hong kong police officers').
'raise'('the sar flag', 'another #').
'leave for'('the royal yacht britannia', 'the #').
'hold by'('the chinese and british governments', 'the handover of hong kong').
'rise over'('this land', 'the regional flag of the hong kong special administrative region of the people \'s republic of china').
'cast eye on'('hong kong', 'the world').
'hold on'('schedule', 'the # governments').
'be festival for'('the chinese nation', 'this').
'go in'('the annals of history', 'july # , #').
'become master of'('this chinese land', 'the hong kong compatriots').
'enter era of'('development', 'hong kong').
'remember'('mr deng xiaoping', 'history').
'be along'('the course', 'it').
'resolve'('the hong kong question', 'we').
这是什么原因?
有没有网站可以演示我的正则表达式应用于 java 模式匹配器?喜欢 regexr.com 或者一些简单易懂的文档也很好,我的 google 搜索结果非常零散且不连贯。
因为所有的逗号后面都有一个space。
所以我建议你使用\s*
(匹配零个或多个spaces)或\s?
(匹配可选的 space),
Pattern p = Pattern.compile("'(.*?)'\('(.*?)',\s*'(.*?)'\)\.");
示例:
'prevents'('scurvy','vitamin C').
^
| - no space
但是
'take place in'('the grand hall of the hong kong convention', 'the ceremony').
^
|- space