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 8 years ago.
Improve this question
"Rubymonk Is Pretty Brilliant".match(/ ./, 9)
How is the answer "P" calculated from this regex?
use the match method on the string
passes two arguments, a regular expression and the position in the string to begin the search.
returns the character 'P'
The criteria you posted from the Rubymonk grader answer this succinctly:
passes two arguments, a regular expression and the position in the
string to begin the search
But let's examine that in more detail. match is being passed two arguments:
/ ./, a regular expression
9, the starting position in the string
The regular expression tells us that we're looking for a space () followed by any character (.).
The starting position tells us to start at position 9 (I). So instead of applying that regex against "Rubymonk Is Pretty Brilliant", we're applying it against "Is Pretty Brilliant".
In the string "Is Pretty Brilliant", where is the first place we encounter a space followed by another character? "Is[ P]retty Brilliant", right? Thus match finds a result of P (that's space-P, matching the regex, not just P.)
To see this more clearly and to experiment further with regexes, you can try it in an irb session or in your browser using Rubular.
(Just google for RegEx + ruby, You will find explanation of regex syntax)
/ANYTHING-HERE/
Will look for ANYTHING-HERE in the text.
In Your example its (/ ./,9):
/SPACE DOT/
So it will look for space followed by single character (Dot -> single character).
9 will be "I" from the string. And that is not space, so it will go on 2 characters right. Will find space, and then will find single character "P".
That is the result.
Related
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 3 months ago.
Improve this question
I started learning Ruby from scratch, from the preliminary preparation there is a certain knowledge of HTML and CSS. For training I use Code Academy. I have questions and can't always find an answer I can understand I need help understanding the following:
user_input = gets.chomp
user_input.downcase!
Explain why user_input is equivalent to gets.chomp and what that means, thanks in advance!
In Ruby = is used to assign values to variables, as in:
x = 1
y = x
Where y assumes the value of x at the moment that line is executed. This is not to be confused with "equivalence" as in x=y in a mathematical sense where you're establishing some kind of permanent relationship.
In Ruby methods return a value, even if that value is "nothing", or nil. In the case of gets, it returns a String. You can call chomp on that, or any other thing you need to achieve your objective, like chaining on downcase.
On its own gets.chomp will read a line of input, strip off the trailing linefeed character, and then throw the result in the trash. Assigning this to a variable preserves that output.
To understand it, break it down first
Accept user input
Clean the user input (using chomp https://apidock.com/ruby/String/chomp)
Downcase it
user_input = gets # will return the value entered by the user
user_input = user_input.chomp # will remove the trailing \n
# A more idiomatic way to achieve the above steps in a single line
user_input = gets.chomp
# Finally downcase
user_input.downcase!
# By that same principle the entire code can be written in a single line
user_input = gets.chomp.downcase
user_input is equivalent to gets.chomp
Remember, everything in Ruby is an object. So gets returns a String object, so does chomp and so does downcase. Hence with this logic you are essentially calling instance methods on the String class
String.new("hello") == "hello" # true
# "hello".chomp is same as String.new("hello").chomp
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 2 years ago.
Improve this question
For a given String in Swift I need to remove prefix and suffix characters that belong to a predefined character set.
I can use components(separatedBy:) with the character set and get rid of the empty strings at the beginning and end of the components array. Just wondering if there's a better approach?
Thanks!
You can use .trimmingCharacters(in: CharacterSet) on the string. From the 'help' text in Xcode on this function: "Returns a new string made by removing from both ends of the String characters contained in a given character set."
Here is a unit test example using a custom character set. Notice it only removes characters from the start and end, not the middle (the comma stays):
import XCTest
class TrimCharacters: XCTestCase {
func testExample() throws {
let string = "abcbaHello, World!ccbbaa"
let charactersToTrim = CharacterSet(charactersIn: "abc,")
XCTAssertEqual(string.trimmingCharacters(in: charactersToTrim), "Hello, World!")
}
}
There are also predefined character sets that can be useful. You can also invert a character set. See CharacterSet
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I need to create a regex which contains at least 1 special character, and at least 1 number with alphabets.
You may try the following pattern:
^(?=.*[0-9])(?=.*[^A-Za-z0-9])(?=.*[A-Za-z]).*$
Explanation:
(?=.*[0-9]) assert one number present
(?=.*[^A-Za-z0-9]) assert one special character present
(?=.*[A-Za-z]) assert one alpha present
Note that I have defined a special character as being anything not alphanumeric. If instead you have a list of special characters, then you can modify the middle lookahead in my pattern.
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've been using this:
if value.chars.count < value.bytes.count
puts "Some non english characters found."
end
But this incorrectly marks the following as non-English.
React and You: A Designer’s Point of View
How can I easily check if a string has no Asian/French/Russian characters?
I can probably iterate through each char in the string and if .bytes == 1 add it to a temp var. Then if that temp var is not nil it means it's an English character. But this seems rather convoluted.
As pointed out in the comments (here and here), this solution will reject some english words with letters that may be considered as "non English" characters.
Using the answer provided in "How to read only English characters" you could adjust it to remove any punctuation character or space, and make the comparison wit that same regex, something like this:
str = "React and You: A Designer’s Point of View"
str.gsub(/[[:punct:]]|\s/, "") =~ /^[a-zA-Z]+$/
#=> 0
.gsub(/[[:punct:]]|\s/, "") will remove any punctuation character or space, so you can compare that with the /^[a-zA-Z]+$/ regexp.
Here are step by step examples:
str = "React and You: A Designer’s Point of View"
str.gsub!(/[[:punct:]]|\s/, "") #=> "ReactandYouADesignersPointofView"
str =~ /^[a-zA-Z]+$/ #=> 0
str = "Comment ça va?"
str.gsub!(/[[:punct:]]|\s/, "") #=> "Commentçava"
str =~ /^[a-zA-Z]+$/ #=> nil
If you are expecting numbers too, then change the regexp to: /^[a-zA-Z0-9]+$/.
As pointed out in this comment, note that using [[:punct:]] will allow non-english punctuation characters such as ¿ or ¡; so, if those characters are also expected (and must cause to reject the sentence as valid), then maybe it is better to avoid gsub and compare to a custom regex with all allowed characters, for example1:
str =~ /^[a-zA-Z0-9\[\]{}\\*:;#$%&#?!|’'"-\.\/_\s]+$/
1 This is just an example with most common characters that i could think of, but needs to be customized with any character considered as valid.
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 7 years ago.
Improve this question
am working on a weather app and this question jump in my mind
original string
<span class="phrase">
string with backslash
"<span class=\"phrase\">"
You have double quotes to open and close the string. If you have a double quote halfway, the parser will think that the string ends there. Adding the backslash ('escaping' the quote) tells the parser that the quote should be interpreted as a literal quote within the string rather than a string terminator.
For more information see Swift: strings and characters (scroll down to 'Special Characters in String Literals').
The backslash is used as an escape character - a " in a string literal means 'end of the string' in Swift, so you need another character - the backslash - to tell Swift 'interpret this " literally - I want it to be part of the string.'.
Of course, in HTML you can specify the value of an attribute with a single quote as well, so
"<span class=\"phrase\">"
and
"<span class='phrase'>"
will have the same effect.