0

I do not understand much about regular expressions. I hope you can help me.

I have in a file the date of some files

Aug 15:07 file1.txt
Jul 15:04 file2.txt
Aug 15:05 file3.txt
Aug 15:05 file4.txt
Aug 2016 file5.txt
Jul 15:09 file6.txt

I want only files that start with 'Aug' AND have the time (:)

/^Aug :/

But doing that shows me all the files without exception

Danielle
  • 89
  • 3
  • 9

2 Answers2

1

This will look for filenames starting with 'Aug :' an this is not what you want.

Try /^Aug [0-9]{1,2}:/ instead. It looks for filenames which:

  • start with 'Aug '
  • then one or two digits
  • then the colon

Also, what is the bash command you're running ?

(You can test your regex at regex101.com, it's a good website or that ;-) )

Valentin Coudert
  • 1,759
  • 3
  • 19
  • 44
1

Use this Pattern ^Aug\s+[0-9]+:[0-9]+\s+(.+$) Demo

^           # Start of string/line
Aug         # "Aug"
\s          # <whitespace character>
+           # (one or more)(greedy)
[0-9]       # Character in [0-9] Character Class
+           # (one or more)(greedy)
:           # ":"
[0-9]       # Character in [0-9] Character Class
+           # (one or more)(greedy)
\s          # <whitespace character>
+           # (one or more)(greedy)
(           # Capturing Group (1)
  .         # Any character except line break
  +         # (one or more)(greedy)
  $         # End of string/line
)           # End of Capturing Group (1)
alpha bravo
  • 7,838
  • 1
  • 19
  • 23