[英]Regular expression for match
How to find such lines in a file 如何在文件中找到这样的行
######## this_is_a_line.sh ########
I tried the below regular expression but it does not work 我尝试了下面的正则表达式,但它不起作用
(#)+( )+(A-Za-z0-9_)+(.sh)( )+(#)+
But this doesn't seem to work. 但这似乎不起作用。 Can anyone let me know what is wrong?
任何人都可以让我知道什么是错的?
You used (...)
(a capturing group ) instead of [...]
(a character class ). 您使用
(...)
( 捕获组 )而不是[...]
( 字符类 )。 Use the character class: 使用字符类:
#+ +[A-Za-z0-9_]+\.sh +#+
^^^^^^^^^^^^^
See regex demo (note most of the capture groups are redundant here, and I removed them. Also, .
must be escaped to match a literal dot.) 见正则表达式演示 (注意最捕获组是多余的这里,和我删除它们。此外,
.
必须逃到匹配一个点。)
The [A-Za-z0-9_]+
matches 1 or more letters, digits or _
. [A-Za-z0-9_]+
匹配1个或多个字母,数字或_
。
The (A-Za-z0-9_)+
matches 1 or more sequences of A-Za-z0-9_
(see demo ). (A-Za-z0-9_)+
匹配A-Za-z0-9_
的A-Za-z0-9_
或多个序列 (参见演示 )。
Also, in Java, you can use \\w
to match [A-Za-z0-9_]
and shorten your regex to 此外,在Java中,您可以使用
\\w
匹配[A-Za-z0-9_]
并将正则表达式缩短为
#+ +\w+\.sh +#+
Do not forget that you need to double each \\
in the pattern string in Java ( String pattern = "#+ +\\\\w+\\\\.sh +#+";
). 不要忘记你需要在Java中的模式字符串中将每个
\\
加倍( String pattern = "#+ +\\\\w+\\\\.sh +#+";
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.