Regex that Detects specific string pattern - ios

I'm new to Regex. The question asks me, Come up with a Regex that Detects specific string whether in start, middle or end of a string with Regular Expressions?
Try it yourself before checking the solution below:
I first thought the solution would be this:
(?:^\s+)|(?:\s+$)
The solution is this:
(?:\s|_)ra(?:\s|_)
How does this Regex work?
Can you walk me through each step?
What does this line of Regex do? Please help me out.

I was able to find answer to my question. I will close the post now.

Related

Rails - Alphanumeric field validation

In rails 4, I need to validate the alphanumeric field which can accept only dot(.), hyphen(-), slash(/) and space in between the characters.
Eg: AB123-GH345 or AB45.NH744 or KHJ3/SD34 or HJS23 JKA34
I have tried with /^[0-9]+#$/ and /^\d+([.,]\d+)?$/ and /^[0-9]+#$/ but it is not working as per the requirement.
Value should be accept as per the examples. Please help me to validate this field.
I think this might help you:
/^[A-Za-z0-9-\/\.\s]+$/
This worked for all the examples you have provided
AB123-GH345 or AB45.NH744 or KHJ3/SD34 or HJS23 JKA34
and rejected when I inserted a character like ? in the middle(HJS23?JKA34).
Update
If you don't want multiline anchors then you can use it like this:
/\A[A-Za-z0-9-\/\.\s]+\z/
You can use this Rubular site to validate your Regex codes.
Try this:
/^[a-zA-Z\d\.\-\/# ]+$/
try this regex
^[a-zA-Z0-9\. /'\-]+$
Hope this will help

Regex to normalize topic links in Discourse forum

I am using Discourse forum software. As in its current state, Discourse presents links to topic in two ways, with and without a post number at the end.
Example:
forum.domain.com/t/some-topic/23
forum.domain.com/t/some-topic/23/5
The first one is what I want and the second one I want to not be displayed in the forum at all.
I've written a post about it on Discourse forum but didn't receive an answer what Regex to put in the permalink normalization input field in the admin section.
I was told that there is an option to do it using permalink normalization like so (It's an example shown in the admin under the Regex input text, I didn't write it):
permalink normalizations
Apply the following regex before matching permalinks,
for example: /(topic.)\?./\1 will strip query strings from topic routes.
Format is regex+string use \1 etc. to access captures
I don't know what Regex I should use in order to remove the numerical value of the post number from links. I need it only for topic links.
This is the routes.rb routing library and this is the permalink.rb library (I think that the permalink library should help get a better clue how to achieve this). I have no idea how to approach this, because it seems that I need some knowledge of the Discourse routing to make it work. For example, I don't understand why (topic.) is part of the regex, what does it mean, so their example doesn't help me to find a solution.
In the admin I have an input field in which I nee to put the normalization regex code.
I need help with the Regex. I need the regex to work with all topics.
Things I've tried that didn't work out:
/(\/\d+)\/\d+$/\1
/(t/[^/]+/\d+).*/\1
/(\/\d+)\/[0-9]+$/\1
/(\/\d+)\/[0-9]+/\1
/(\/\d+)\/\d+$/\1/
/(forum.domain.com(\/\w+)*\/\d+)\/\d+(?=\s|$)/\1
Note: The Permalink Normalization input field treats the character | as a separator to separate between several Regex expressions.
I think this may be the expression you are looking for to put inside de settings field:
/(t\/.*\/\d+)(\/\d+)/\1
You can see it working on Rubular.
However, the code that generates the url is not using the normalization code, so the expression is being ignored.
You could try normalizing the permalink there:
def last_post_url
url = "#{Discourse.base_uri}/t/#{slug}/#{id}/#{posts_count}"
url = Permalink.normalize_url url
url
end
I didn't truly understand your question, but if I got it right, you are saying that you want links with /some-number at the end but don't what links with /some-number/some-number at the end. If that is the case, the regex is:
forum\.domain\.com\/t\/[^0-9\/]+\/\d{1,9}$
You can replace 'forum' with your forum name and 'domain' with your domain name.
This will remove trailing "/<digits>" after another "/<digits>":
/(forum.domain.com(\/\w+)*\/\d+)\/\d+(?=\s|$)/\1

Deep Link Kit regex for zero or more

I am trying to use Deep Link Kit to route both of these paths:
myapp://page/2 // <- doesn't work
myapp://page/2/7 //<- works
The route handler I've registered at the moment is:
router.registerHandlerClass(AppRouteHandler.self, forRoute: "page/:number/:commentID(.*)")
I added the (.*) for the regex of zero or more comment IDs. However this doesn't seem to make any difference as it only works when you have both the :number and :commentID defined. I've also tried myapp://page/2/ but that doesn't work either. Any help would be appreciated.
UPDATE
One solution is to register the two routes separately:
router.registerHandlerClass(AppRouteHandler.self, forRoute: "page/:number")
router.registerHandlerClass(AppRouteHandler.self, forRoute: "page/:number/:commentID")
but ideally, I'd be able to use regex.
I counter this problem too, after combine you solution I came up with this solution
router.registerHandlerClass(AppRouteHandler.self, forRoute: "page/:number/?:commentID(.*)")
That will ignore the second /, and your commentID will be set with empty string

Regular expression not working when put in an object

I'm trying to store regexes in a database but they're not working when used in a .sub(), even though the same regex works when used directly in .sub() as a string.
regex = Class.object.field // Class.object is an active record containing "\w*\s\/\s"
mystring = "first / second"
mystring.sub(/#{regex}/, '')
// => nil
mystring.sub(/\w*\s\/\s/, '')
// => second
Any insight appreciated!
Thanks,
Matt.
Editing to correct class/object terminology (thanks) & correcting my 2nd example as I had shown #{} wrapped around the working regex (cut & paste SNAFU).
To answer your question: It is not quite what kind of thing your Class.object is. If it's an ActiveRecord, it won't work.
Edit: You obviously found that the problem is Rails escaping the regexp.
An ActiveRecord cannot "contain" your regular expression directly; the regexp will be in one of the fields of your record. In which case you'd want to do something like regexp = Class.object.field_containing_the_regexp.
Even if that is not the case, I suspect that the problem is that your regexp is something other than a string. You can quickly test this by using
puts "My regexp: #{regexp}"
The string that you will see in the output will be the one that is used for the regexp.
A String is not a Regexp. You have to create a Regexp object first.
regex = Regexp.new("\w*\s\/\s")
Turns out my regexp didn't cater for all cases - \w didn't account for symbols. After checking in rails console, and seeing the screwey escaping I was alreasdy half-way down the wrong track.
Thanks for the help.

elaborate urls with regex

i have a string that maybe contains text with links.
I use these instructions for elaborate it:
message = message.gsub(/http[s]?:\/\/[^\s]+/) do |m|
replace_url(m)
end
if the string is "http://www.youtube.com/watch?v=6zToqLlM8ms&playnext_from=TL&videos=qpCvM5Ocr3M&feature=sub"
the instructions works.
but if the string is "hi my video is http://www.youtube.com/watch?v=6zToqLlM8ms&playnext_from=TL&videos=qpCvM5Ocr3M&feature=sub"
doesn't works
why?
how can i do?
thanks
Beucase this not match a pattern. Get before url add expression to catch some crap before url.
You can also try match a regex pattern to every word. Because url is always one word separated word.

Resources