Match regex pattern only in filename.extension

723

You mention that ls with grep would work if you got the full path to the file, how about find and grep? Because grep returns a line if a match is found anywhere in that line, your regex can match something that lacks /s before the filename and still contains your search string.

$ find folder | grep -i '[^/]*love[^/]*\.[a-z0-9]*'

That seems to work for me. The extension match can be reduced if you know all of your extensions lack numbers or if you know all of the files you want to match have a specific extension.


Example usage

$ find folder
folder
folder/a-love-song.ogg
folder/love-songs
folder/love-songs/a-blues-song.ogg
folder/love-songs/another-love-song.ogg
$ find folder | grep -i '[^/]*love[^/]*\.[a-z0-9]*'
folder/a-love-song.ogg
folder/love-songs/another-love-song.ogg

Attempted explanation of regex

  • [^/]*: Matches any number of characters that are not /
  • love[^/]*: Matches the string love and then any number of characters that are not /
  • \.[a-z0-9]*: Matches a . and then any number of letters or digits

Put together, [^/]*love[^/]*\.[a-z0-9]* matches with as many non-/ characters on either side of love with an extension tacked onto the end. Given the string a/love/path/love-file.ogg the above regex only matches love-file.ogg. If a leaf-folder contains love, for example: a/path/love-leaf, it does not match because love-leaf contains no . followed by letters or numbers.

Share:
723

Related videos on Youtube

user5079994
Author by

user5079994

Updated on September 18, 2022

Comments

  • user5079994
    user5079994 over 1 year

    I am trying to make the footer slide in/out depending on the scrolling. So far the only thing I got to work is to show the footer with psYReachEnd, but how do I "tell" the footer to slide back out when I scrolled for ex 100px up ?

    public onReachEnd(): void {
    this.zone.run(() => {
      this.status = true;
    });
    console.log('show footer');}
    
    
    
    
    <perfect-scrollbar (psYReachEnd)="onReachEnd()"><div>content</div><div id="footer" class="container-fluid" [ngClass]="status ? 'show' : 'hide'">footer content</div></perfect-scrollbar>
    
    • Usama Jarral
      Usama Jarral over 4 years
      You can use psScrollUp event and check manually the position and hide the footer
    • user5079994
      user5079994 over 4 years
      could you please elaborate?...I am an angular total newbie
    • Usama Jarral
      Usama Jarral over 4 years
      (psScrollUp)="onScrollUp($event)" and create a method onScrollUp(event) and check event properties you will find scroll position there
    • Usama Jarral
      Usama Jarral over 4 years
      You can check the current scroll position by event.target.scrollTop and find the maximum scroll height by event.target.scrollHeight and write your logic easily
    • user5079994
      user5079994 over 4 years
      still can't figure this out
  • Alex
    Alex about 11 years
    That did not returned anything (yes I replaced dir with the directory).
  • user1686
    user1686 about 11 years
    Fixed the regex.
  • Rain
    Rain about 11 years
    @grawity This still returns dir/love-songs (or any leaf directory with love in it). I'm not sure if it is possible to what @Alex wishes with just find alone.
  • Alex
    Alex about 11 years
    Neat. Do you mind explaining what sorcery you did there? I mean, the regex part.
  • Rain
    Rain about 11 years
    @Alex I can try! If you're still confused, I recommend regexpal.com. It is a great resource to visualize regular expressions and what they match.