Regular expression to match

How to find such lines in a file

########     this_is_a_line.sh     ######## 

I tried the following regex but it doesn't work

(#)+( )+(A-Za-z0-9_)+(.sh)( )+(#)+ 

But this does not seem to work. Can someone tell me what is wrong?

+4
source share
1 answer

You used (...)(a capture group ) instead of [...](a character class ). Use a character class:

#+ +[A-Za-z0-9_]+\.sh +#+
    ^^^^^^^^^^^^^

See regex demo (note that most capture groups are redundant here, and I deleted them. In addition, the .literal dot must be escaped.)

[A-Za-z0-9_]+matches 1 or more letters, numbers or _.

(A-Za-z0-9_)+ 1 A-Za-z0-9_ (. ).

, Java \w [A-Za-z0-9_]

#+ +\w+\.sh +#+

, \ Java (String pattern = "#+ +\\w+\\.sh +#+";).

+6

Source: https://habr.com/ru/post/1625479/


All Articles