regular expression for getting single occourance of a character from a string [closed] - ruby-on-rails

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have some alphanumeric strings. From that I have to find out those strings which satisfy the following condition,
There should be only one character in the whole string and that should be 'e'
'e' should not present at the beginning or end of the string it should be present at the middle.
I want to pick strings like 43e4234,435345e5
I can do the same thing in ruby, but as i have huge number of strings i want to go with regular expression only

This should work:
/\A[^a-z]+e[^a-z]+\z/i
It means :
Beginning of the string
at least one non-letter
'e'
at least one non-letter
end of string
Here's an example :
https://regex101.com/r/H9oza7/1
Use /^[^a-z]+e[^a-z]+$/im if you want to match lines inside a string.

Related

What is the rule for multiple methods(?) on and object (i.e. num.to_s.chars.map{|x| x.to_i**2}.join.to_i)? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
What is the structural rule of something like this? I'm newer to programming and I don't know the technical term for the ".something's" (methods?).
But, in this example, there are 5 (to_s, chars, map, join, and to_i).
num.to_s.chars.map{|x| x.to_i**2}.join.to_i
Basically, all I am wondering is, what is the structure to building these? I've tried doing some similar and have received errors. So, is there a specific order or structure to these? And is the correct term method?
Ideally you should first get fundamental of ruby language. Ruby is one of the easiest language to get hold on. Checkout https://try.ruby-lang.org and you will better understand following.
It's an expression where there is chain of methods being called on the result of each expression.
Assuming num is an integer, see the comment below
num
.to_s # to_s on any ruby object converts it to string
.chars # returns individual characters in string array
.map { |x| # iterates over each number character in array
x.to_i**2 # and convert each character to integer and sqare it( ** is exponent operator)
}
.join # map returns new array and join/conctenate each number
.to_i # convert it back to integer
so if num is 123, it returns 149 which essentially each number is squared.
You can try yourself by running this code one by one in irb

In Ruby, how do I find all the letters after the last number in a string? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm using Ruby 2.4. I have a string with letters and numbers, something like
str = "123abc234abb"
How do I find all the letters occurring after the last number in the string? For example, if I applied the function to the above, it would yield
abb
You could use a positive lookbehind:
"123abc234abb"[/(?<=\d)?[a-zA-Z]+\z/]
#=> "abb"
Try this
str.rpartition(/\d+/).last
How does this work?
rpartition splits the string into three parts, using reverse matching
last picks the post-match part from the three results

Regular Expression in MVC5 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
What would a Regular Expression need to allow characters and numbers only, no special characters or spacing in asp.net mvc5?
You generally use ranges such as [a-z] and [0-9] to filter out just characters and numbers with an asterisk after it *
I don't have a copy of MVC 5 handy so I don't know what the particular syntax is.
A regex for that often looks like:
([0-9]|[A-Z]|[a-z])*
It will be very similar in asp.net or mvc, likely.
That searches for all alphabetic characters from a to z, and all numbers from 0 to 9. The asterisk makes it search for multiple characters and not just a single character at a time. The pipe character says "or". Search for characters upper case, or characters lower case, or numbers. The brackets help sort groups.
As I said though you will have to figure it out the specific syntax of your regex library that your programming language uses, as they can differ. There are perl style regexes, and many variations. The above is just a sample. You can test at:
http://regexstorm.net/tester

Ruby - converting a hashtag to actual word(s) ? (#contentmarketing => content marketing) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Hashtags sometimes combine two or more words, such as:
content marketing => #contentmarketing
If I have a bunch of hashtags assigned to an article, and the word is in that article, i.e. content marketing. How can I take that hash tag, and detect the word(s) that make up the hashtag?
If the hashtag is a single word, it's trivial: simply look for that word in the article. But, what if the hash tag is two or more words? I could simply split the hashtag in all possible indices and check if the two words produced were in the article.
So for #contentmarketing, I'd check for the words:
c ontentmarketing
co ntentmarketing
con tentmarketing
...
content marketing <= THIS IS THE ANSWER!
...
However, this fails if there are three or more words in the hashtags, unless I split it recursively but that seems very inelegant.
Again, this is assuming the words in the hash tag are in the article.
You can use a regex with an optional space between each character to do this:
your_article =~ /#{hashtag.chars.to_a.join(' ?')}/
I can think of two possible solutions depending on the requirements for the hashtags:
Assuming hashtags must be made up of words and can't be non-words like "#abfgtest":
Do the test similar to your answer above but only test the first part of the string. If the test fails then add another character and try again until you have a word. Then repeat this process on the remaining string until you have found each word. So using your example it would first test:
- c
- co
- ...
- content <- Found a word, start over with rest
- m
- ma
- ...
- marketing <- Found a word, no more string so exit
If you can have garbage, then you will need to do the same thing as option 1. with an additional step. Whenever you reach the end of the string without finding a word, go back to the beginning + 1. Using the #abfgtest example, first you'd run the above function on "abfgtest", then "bfgtest", then "fgtest", etc.

Regular expression for password validation that doesn't allow spaces [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I need a regular expression for my Ruby on Rails application for the password field.
Any character or number or symbols is allowed except space.
If this is client-side validation in Javascript (or any language other than Ruby), this expression will match a string with no whitespace (\S) at least one character (+), no max:
^\S+$
Ruby is the only language that uses multi-line mode by default, so the start-of-line ^ and end-of-line $ behave differently (they match once per input, no matter how many lines). So, if you are validating the input in Ruby, you'd need to use \A for start-of-line and \Z for end-of-line.
\A\S+\Z
All except spaces, do you need to narrow the results a bit more than this?
/[^ ]+/
This is without minimum length (or rather, with minimum length 1):
^\S+$
With minimum length 8:
^\S{7}\S+$
or, if your regex engine supports it (don't know why it wouldn't):
^\S{8,}$

Resources