简体   繁体   English

如何创建正则表达式以匹配Java中单词的结尾?

[英]How to create regular expression to match the end of word in Java?

Need regex for matching { character and every line that ends with this character. 需要正则表达式来匹配{字符以及以该字符结尾的每一行。 For example: (isMoving){ 例如: (isMoving){

Tried: 尝试过:

^.?\{$

but it works only for single { 但仅适用于单个{

.? means zero or one character. 表示零或一个字符。 You want to match zero or more. 您要匹配零个或多个。 Also the curly brace is a special regex character that is used for quantifying the occurrences of a certain pattern, and so it should be escaped: 花括号也是一个特殊的正则表达式字符,用于量化某种模式的出现,因此应将其转义:

^.*\{$

The Java string representation of the regex you want is ".*\\\\{" for use like 您想要的正则表达式的Java字符串表示形式是".*\\\\{" ,用于类似

if (lineText.matches(".*\\{")) { ... }

A good place to experiment with such things is RegexPlanet's Java page RegexPlanet的Java页面是一个尝试此类事情的好地方

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

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