Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6117675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:20:53+00:00 2026-05-23T15:20:53+00:00

The following instance method takes a file path and returns the file’s prefix (the

  • 0

The following instance method takes a file path and returns the file’s prefix (the part before the separator):

@separator = "@"

def table_name path
  regex = Regexp.new("\/[^\/]+#{@separator}")
  path.match(regex)[0].gsub(/^.|.$/,'').downcase.to_sym
end

table_name "bla/bla/bla/Prefix@invoice.csv"
# => :prefix

So far, this method only works on Unix. To make it work on Windows, I also need to capture the backslash (\). Unfortunately, that’s when I got stuck:

@separator = "@"

def table_name path
  regex = Regexp.new("(\/|\\)[^\/\\]+#{@separator}")
  path.match(regex)[0].gsub(/^.|.$/,'').downcase.to_sym
end

table_name("bla/bla/bla/Prefix@invoice.csv")
# RegexpError: premature end of char-class: /(\/|\)[^\/\]+@/

# Target result:
table_name("bla/bla/bla/Prefix@invoice.csv")
# => :prefix
table_name("bla\bla\bla\Prefix@invoice.csv")
# => :prefix

I suspect Ruby’s string interpolation and escaping is what confuses me here.

How could I change the Regex to make it work on both Unix and Windows?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:20:54+00:00Added an answer on May 23, 2026 at 3:20 pm

    I don’t actually know what bla/bla/bla/Prefix@invoice.csv refers to; is bla/bla/bla/bla all directories, and the filename Prefix@invoice.csv?

    With the assumption that I’ve correctly understood your filenames, I suggest using File.split():

    irb> (path, name) = File.split("bla/bla/bla/Prefix@invoice.csv")
    => ["bla/bla/bla", "Prefix@invoice.csv"]
    irb> (prefix, postfix) = name.split("@")
    => ["Prefix", "invoice.csv"]
    

    Not only is it platform-agnostic, it is more legible too.

    Update

    You piqued my curiosity:

    >> wpath="blah\\blah\\blah\\Prefix@invoice.csv"
    => "blah\\blah\\blah\\Prefix@invoice.csv"
    >> upath="bla/bla/bla/Prefix@invoice.csv"
    => "bla/bla/bla/Prefix@invoice.csv"
    >> r=Regexp.new(".+[\\\\/]([^@]+)@(.+)")
    => /.+[\\\/]([^@]+)@(.+)/
    >> wpath.match(r)
    => #<MatchData "blah\\blah\\blah\\Prefix@invoice.csv" 1:"Prefix" 2:"invoice.csv">
    >> upath.match(r)
    => #<MatchData "bla/bla/bla/Prefix@invoice.csv" 1:"Prefix" 2:"invoice.csv">
    

    You’re right, the \ must be double-escaped for it to work in a regular expression: once to get past the interpreter, again to get past the regex engine. (Definitely feels awkward.) The regex is:

    .+[\\/]([^@]+)@(.+)
    

    The string is:

    ".+[\\\\/]([^@]+)@(.+)"
    

    The regex, which might be too brittle for real use (how would it handle a path without / or \ path separators or a pathname without @ or with too many @?), looks for any number of characters, a single path separator, any amount of non-@, an @, then any amount of any characters. I’m assuming that the first .+ will greedily consume as many characters as possible to make the match as far to the right as possible:

    >> evil_path="/foo/bar@baz/blorp/Prefix@invoice.csv"
    => "/foo/bar@baz/blorp/Prefix@invoice.csv"
    >> evil_path.match(r)
    => #<MatchData "/foo/bar@baz/blorp/Prefix@invoice.csv" 1:"Prefix" 2:"invoice.csv">
    

    But depending upon malformed input data, it might do the very wrong thing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For instance, is the following possible: #define definer(x) #define #x?
For instance, my query is like the following using SQL Server 2005: SELECT *
I'm managing an instance of Wordpress where the URLs are in the following format:
I want to create the following element: <exercises xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="mySchema.xsd"> If I use something
I am attempting to pull some information from my tnsnames file using regex. I
I have an init method that takes values from a NSDictionary . This is
I have the following code in my class method for test: NSMutableDictionary * temp
I'd like your opinion on the following subject: Imagine we have a method that
i'm using Spring-WS and have the following spring-ws-servlet.xml file. The injection of defaultURI and
I have defined a method called ccy which takes in a number num ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.