I have a a number of routes that can be like :
possible routes:
- mac-book-retina-17-pid234-234
- hp-laptop-pid234-234
- vaoe-x12-pid234-234
and I want to match all to one action using the constraints in Ruby route file. Something like
get 'products/:product_info', to: 'products#type', constraints: { product_info: /[a-z]+-a-z]+-a-z]+-pid\d+-\d+/ }
The problem is that the /[a-z]+-/ can get repeated 1 time, 2 times and 3 times, and it makes it hard to get a consistent shared Regex for all the cases.
The only part that is constant in all routes is the last part: pid234-234 which refers to the product id and another sub_id.
I am thinking of something like: find all strings untill you each this part(pid), but I do not know how to do that.
I would say a good place to start is dynamic-segments
get 'products/:product_info', to: 'products#type', constraints: { product_info: /[A-Z]\d{5}/ }
I hope that this helps
Happy Hacking
I think I managed to find a possible solution for this:
(.*)pid\d+-\d+
this regex will match all the strings until it reaches the pid-12-12.
Related
I am creating a test that can be expressed as a disjunction of assertions; when the first assertion fails, then it should look at the next. Particularly, an item will be equal to one of two things. Which one, I do not know.
My code looks something like this. It does not work but it might give you an idea of what I am trying to do.
asset_one = Cache.asset_one
asset_two = Cache.asset_two
assert_equal(asset_one.name, Pages.name) or
assert_equal(asset_two.name, Pages.name)
The Pages.name should match one of the targets. I don't know which one. If it doesn't match the first, then I want it to skip it and try to match the second.
Any help is as always much appreciated.
If you want to match any one of the object having the given name, you can use assert_includes
asset_one = Cache.asset_one
asset_two = Cache.asset_two
assert_includes([asset_one, asset_two].map(&:name), Pages.name)
This should solve your purpose.
I am wanting to solve this problem, but am kind of unsure how to correctly structure the logic for doing this. I am given a list of user names and I am told to find an extracted name for that. So, for example, I'll see a list of user names such as this:
jason
dooley
smith
rob.smith
kristi.bailey
kristi.betty.bailey
kristi.b.bailey
robertvolk
robvolk
k.b.dula
kristidula
kristibettydula
kristibdula
kdula
kbdula
alexanderson
caesardv
joseluis.lopez
jbpritzker
jean-luc.vey
dvandewal
malami
jgarciathome
christophertroethlisberger
How can I then turn each user name into an extracted name? The only parameter I am given is that every user name is guaranteed to have at least a partial person's name.
So for example, kristi.bailey would be turned into "Kristi Bailey"
alexanderson would be turned into "Alex Anderson"
So, the pattern I see is that, if I see a period I will turn that into two strings (possibly a first and last name). If I see three periods then it will be first, middle. The problem I am having trouble finding the logic for is when the name is just clumped up together like alexanderson or jgarciathome. How can I turn that into an extracted name? I was thinking of doing something like if I see 2 consonants and a vowel in a row I would separate the names, but I don't think that'll work.
Any ideas?
I'd use a string.StartsWith method and a string.EndsWith method and determine the maximum overlap on each. As long as it's more than 2 characters, call that the common name. Sort them into buckets based on the common name. It's a naive implementation, but it that's where I'd start.
Example:
string name1 = "kristi.bailey";
string name2 = "kristi.betty.bailey";
// We've got a 6 character overlap for first name:
name2.StartsWith(name1.Substring(0,6)) // this is true
// We've got a 6 character overlap for last name:
name2.EndsWith(name1.Substring(7)) // this is true
HTH!
is it possible to create some unique ID for articles on rails?
For example, first article will get ID - aa-001,
second - aa-002
...
article #999 - aa-999,
article #1000 - ab-001 and so on?
Thanks in advance for your help!
The following method gives the next id in the sequence, given the one before:
def next_id(id, limit = 3, seperator = '-')
if id[/[0-9]+\z/] == ?9 * limit
"#{id[/\A[a-z]+/i].next}#{seperator}#{?0 * (limit - 1)}1"
else
id.next
end
end
> next_id("aa-009")
=> "aa-010"
> next_id("aa-999")
=> "ab-001"
The limit parameter specifies the number of digits. You can use as many prefix characters as you want.
Which means you could use it like this in your application:
> Post.last.special_id
=> "bc-999"
next_id(Post.last.special_id)
=> "bd-001"
However, I'm not sure I'd advice you to do it like this. Databases have smart methods to avoid race conditions for creating ids when entries are created concurrently. In Postgres, for example, it doesn't guarantee gapless ids.
This approach has no such mechanism, which could potentially lead to race conditions. However, if this is extremely unlikely to happen such in a case where you are the only one writing articles, you could do it anyway. I'm not exactly sure what you want to use this for, but you might want to look into to_param.
You may want to look into the FriendlyId gem. There’s also a Railscast on this topic which covers a manual approach as well as the usage of FriendlyId.
As part of a chat app I'm writing, I need to use regular expressions to match asterisks and underscores in chat messages and turn them into <strong> and <em> tags. Since I'm terrible with regex, I'm really stuck here. Ideally, we would have it set up such that:
One to three words, but not more, can be marked for strong/em.
Patterns such as "un*believ*able" would be matched.
Only one or the other (strong OR em) work within one line.
The above parameters are in order of importance, with only #1 being utterly necessary - the others are just prettiness. The closest I came to anything that worked was:
text = text.sub(/\*([(0-9a-zA-Z).*])\*/,'<b>\1<\/b>')
text = text.sub(/_([(0-9a-zA-Z).*])_/,'<i>\1<\/i>')
But it obviously doesn't work with any of our params.
It's odd that there's not an example of something similar already out there, given the popularity of using asterisks for bold and whatnot. If there is, I couldn't find it outside of plugins/gems (which won't work for this instance, as I really only need it in in one place in my model). Any help would be appreciated.
This should help you finish what you are doing:
sub(/\*(.*)\*/,'<b>\1</b>')
sub(/_(.*)_/,'<i>\1</i>')
Firstly, your criteria are a little strange, but, okay...
It seems that a possible algorithm for this would be to find the number of matches in a message, count them to see if there are less than 4, and then try to perform one set of substitutions.
strong_regexp = /\*([^\*]*)\*/
em_regexp = /_([^_]*)_/
def process(input)
if input ~= strong_regexp && input.match(strong_regexp).size < 4
input.sub strong_regexp, "<b>\1<\b>"
elsif input ~= em_regexp && intput.match(em_regexp).size < 4
input.sub em_regexp, "<i>\1<\i>"
end
end
Your specifications aren't entirely clear, but if you understand this, you can tweak it yourself.
i'm setting up a directory structure based on dates: 2010/02/01
right now, my rewrite rules look something like this:
([0-9]{4})/([0-9]{2})/([0-9]{2})
I tried limiting the ranges - the month, for instance: ([01-12]{2}) - but that doesn't seem to work. is there a way to do this, or am i making this too complicated and i shouldn't worry about it?
i don't want something like: (01|02|03...10|11|12)
thanks!
Regular expressions don't see numbers as numbers. It sees each individual digit as a character. So, [01-12] would actually equivocate to [012] or [0-2]. (Someone correct me if I'm wrong on that.)
I'm not a master at RexEx, so someone might have a better solution, but here's what I'd use:
(2\d{3})/(1[0-2]|0[1-9])/(3[0-1]|[1-2]\d|0[1-9])
Untested, but that should restrict your year to the 2000s, your months to 01-12, and your days to 01-31.