Advertisement
Guest User

Untitled

a guest
Jun 21st, 2016
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. # Slightly improved the pattern:
  2. s/\/([^/]+\/)*([^/]*)/\2/g
  3. # The s/a/b/ operator is often written with slashes
  4. # But you could use another character to avoid some backslashing, here %
  5. s%/([^/]+/)*([^/]*)%\2%g
  6. # the choice of delimiter is for readability
  7. # and prevents having to escape the slashes to avoid collision
  8. # s%a%b%g replaces a to b throughout the file
  9. # The pattern is, hence:
  10. / ([^/]+/)* ([^/]*)
  11. # I used spaces to separate, which works on some regex languages, while in others they're taken to be part of the pattern, beware
  12. # First there is a slash, it has no special meaning, so the string to match starts with a slash
  13. # The [...] means "any of the characters listed inside
  14. # The [^...] means the opposite: "any character EXCEPT those listed
  15. # [^/] thus means: any character but a slash
  16. # x+ means one or more occurences of 'x' (consecutive of course)
  17. # [^/]+ therefore means 'one or more not-slash characters
  18. # Then there is a slash and everything is grouped in parentheses
  19. ([^/]+/)
  20. # matches 'foo/' 'x/' but not '/' or 'foo/bar/baz/'
  21. # x* means 'zero or more x'
  22. ([^/]+/)*
  23. # matches all the strings aforementioned. But neither match strings like 'foo' or 'foo/bar'
  24. # remember we had a slash at the beginning?
  25. /([^/]+/)*
  26. # matches any full pathname for directories, such as '/usr/share/src/'
  27. # You could be more succinct with
  28. /.*/
  29. # The dot ( . ) means 'any character whatsoever. The star means 'zero or more'
  30. # lastly we have a familiar pattern:
  31. [^/]*
  32. # zero or more non-slash characters
  33. # the reason for the parenthesis comes later. now
  34. / ([^/]+/)* ([^/]*)
  35. # would be a full pathname for a regular (a non-directory) file: '/usr/bin/emacs'
  36. # Remember that the second part was unnecesarily enclosed in parentheses?
  37. # The s// operator catches everything it matches under parentheses in special variables
  38. # They are named \1 \2 \3 etc.
  39. # the pathname part (minus the first slash) is caught inside \1
  40. # The filename part is caught under \2
  41. # That variable is used for the substitution
  42. s%/([^/]+/)*([^/]*)%\2%
  43. # For every pathname that ends in a regular file, substitute it for just the name of the file
  44.  
  45. # there are other ways to do this, a simpler one, a bit prone to error if you have directory pathnames in your input, would be
  46. s%/.*/%%
  47. # change every pathname ending in a slash to nothing
  48.  
  49. # Also to note, if your input is something like
  50. blah /usr/bin/vim
  51. # there is a match
  52. # if you want to say "lines containing ONLY X" you'd do
  53. ^X$
  54. # lines STARTING with X
  55. ^X
  56. # lines ENDING in X
  57. X$
  58.  
  59. # finally the 'g' at the end of the s// operator
  60. s/a/b/g
  61. # is a flag for the operator, which means 'do for every line in the input
  62. # otherwise it would only operate on the first match
  63.  
  64. # The original regex I posted on lainchan ended with
  65. ([^/]*\..*)
  66. # which matches filenames with a dot like '/home/anon/hello.c' but not '/home/anon/hello'
  67. # You can see I escaped the dot ( \. ) to make it a literal match
  68. # and then used a dot-star ( .* ) for the extension
  69. # another flaw there is that it'd match '/home/anon/emacs.d/' because the last dot matches anything
  70. # after the literal dot the 'd/' get captured.
  71. # again it could be fixed like this:
  72. ([^/]*\.[^/]*)
  73. # or even better
  74. ([^/]*(\.[^/])*)
  75. # Which makes the file extension optional and as well
  76.  
  77. # Sorry if my autism was excessive here.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement