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.
Related
I am building a Rails 5.2 app.
In this app I got outputs from different suppliers (I am building a webshop).
The name of the shipping provider is in this format:
dhl_freight__233433
It could also be in this format:
postal__US-320202
How can I remove all that is before (and including) the __ so all that remains are the things after the ___ like for example 233433.
Perhaps some sort of RegEx.
A very simple approach would be to use String#split and then pick the second part that is the last part in this example:
"dhl_freight__233433".split('__').last
#=> "233433"
"postal__US-320202".split('__').last
#=> "US-320202"
You can use a very simple Regexp and a ask the resulting MatchData for the post_match part:
p "dhl_freight__233433".match(/__/).post_match
# another (magic) way to acces the post_match part:
p $'
Postscript: Learnt something from this question myself: you don't even have to use a RegExp for this to work. Just "asddfg__qwer".match("__").post_match does the trick (it does the conversion to regexp for you)
r = /[^_]+\z/
"dhl_freight__233433"[r] #=> "233433"
"postal__US-320202"[r] #=> "US-320202"
The regular expression matches one or more characters other than an underscore, followed by the end of the string (\z). The ^ at the beginning of the character class reads, "other than any of the characters that follow".
See String#[].
This assumes that the last underscore is preceded by an underscore. If the last underscore is not preceded by an underscore, in which case there should be no match, add a positive lookbehind:
r = /(?<=__[^_]+\z/
This requires the match to be preceded by two underscores.
There are many ruby ways to extract numbers from string. I hope you're trying to fetch numbers out of a string. Here are some of the ways to do so.
Ref- http://www.ruby-forum.com/topic/125709
line.delete("^0-9")
line.scan(/\d/).join('')
line.tr("^0-9", '')
In the above delete is the fastest to trim numbers out of strings.
All of above extracts numbers from string and joins them. If a string is like this "String-with-67829___numbers-09764" outut would be like this "6782909764"
In case if you want the numbers split like this ["67829", "09764"]
line.split(/[^\d]/).reject { |c| c.empty? }
Hope these answers help you! Happy coding :-)
I've been trying to figure out the best way to validate a user entry which is a string with comma separated RGB values. It should only allow strings with no whitespaces and in formats such as these (1,12,123; 225,225,2; 32,42,241...).
I've never used Regex before, but i'm guessing it would be the best solution? I've been playing around on RegexPal and have gotten this string working:
(#([\da-f]{3}){1,2}(\d{1,3}%?,\s?){3}(1|0?\.\d+)\)|\d{1,3}%?(,\s?\d{1,3}%?){2})
However, not having much luck using it in Swift. I get the error "Invalid escape sequence in literal".
Would appreciate any help with using that regex in Swift, or if there's a better regex string/solution to validating the entry. Thanks!
You can use hashtag before the first double quote and after the last double quote in Swift to avoid having to manually add a backslash before any special character. Regarding the regex you are using it would allow the user to enter values above the 255 limit.
The regex below adapted from this post would limit the values from 0-255 and would allow the user enter 1 or more rgb values followed by ";" or "; "
#"^\((((([1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])),){2}(([1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5]))(;|; )?){1,}\)$"#
extension StringProtocol {
var isValidRGB: Bool { range(of: #"^\((((([1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])),){2}(([1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5]))(;|; )?){1,}\)$"#,
options: .regularExpression) != nil }
}
"(200,55,1)".isValidRGB // true
"(10,99,255; 0,0,10)".isValidRGB // true
"(2,2,2;)".isValidRGB // true
"(2,2,2;2)".isValidRGB // false
"(2,2,2;2,2)".isValidRGB // false
"(2,2,254;0,0,0)".isValidRGB // true
"(2,2,256;0,0,0)".isValidRGB // false
Add the Swift code where you define the RegEx to your question.
The other poster likely has identified the problem. (#manzarhaq, you should really post your reply as an answer so the OP can accept it.)
The backslash is a special character in Swift strings. It tells the compiler that the character next is a special character. If you want a literal backslash, you need 2 backslashes in a row. So your regEx string might look like this:
let regExStrin = "(#([\\da-f]{3}){1,2}(\\d{1,3}%?,\\s?){3}(1|0?\\.\\d+)\\)|\\d{1,3}%?(,\\s?\\d{1,3}%?){2})"
Note that using backslashes this way is common to most languages that derive, even loosely, from C. Swift does have some C in its ancestry.
In many C-like languages, \n is a newline character, \t is a tab character, \f is a form-feed, \" is a quotation mark, and \\ is a literal backslash.
(I don't think the \f form feed character is defined in Swift. That harks back to the days of ASCII driven serial printers.)
I'm fairly new to this forum. I am having trouble with manipulating the correct string to achieve this.
Basically, what I'm trying to do is receive an input string like this example:
str = "Say hello to=Stack overflow, Say goodbye to=other resources"
for question, answer in pairs(string.gmatch(s, "(%w+)=(%w+)"))
print(question, answer)
end
I want it to return: question = "Say hello to" and answer = "Stack overflow, question = "Say goodbye to" and so on and so forth. but instead, it picks up the word just before the equal sign and the word just after. I've even tried the * quantifier, and it does the same exact thing.
I've also tried this pattern
[%w%s]*=[%w%s]
I just want to be able to sort this string into a key-value table where the key is all words before each = and the value is all words after that equal but before the comma.
Does anyone have a suggestion?
You can use something like this:
local str = "Say hello to=Stack overflow, Say goodbye to=other resources"
for question, answer in string.gmatch(str..",", "([^=]+)=([^,]+),%s*") do
print(question, answer)
end
"([^=]+)=([^,]+),%s*" means the following: anything except = ([^=]) repeated 1 or more times (+) followed by = and then anything except ',', followed by comma and optional whitespaces (to avoid including them in the next question). I also added comma to the string, so it parses the last pair as well.
To elaborate a bit further per request in the comments: in the expression [^=]+, [=] designates a set with one allowed character (=) and [^=] negates that, so it's a set with any character allowed except = and + allows the set to be repeated 1 or more times.
As #lhf suggested you can use a simpler expression: (.-)=(.-),%s*, which means: take all characters until the first = (- makes matching non-greedy) and then take all characters until the first ,.
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.
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
I have a field that contains titles like "IT Professional" or "DB Administrator".
I want to display this in the middle of a sentence and so need to down-case. Unfortunately, this also downcases the acronyms and I end up with "Thanks for joining a community of it professionals".
A good start would be the solution mentioned by Grantovich below, i.e. specifying my acronyms in config/initializers/inflections.rb:
ActiveSupport::Inflector.inflections do |inflect|
inflect.acronym "IT"
inflect.acronym "DB"
end
The problem with going this route is that firstly, I don't want to store them in lower case as suggested as part of the solution because they are titles and should be stored with capitals. Secondly, they are already defined in uppercase and it would be a bad idea to suddenly make them lower case.
Solution Found: Since I want the title to appear in the middle of a sentence, hence the need for lower case, I solved it by downcasing the title, constructing the sentence and then calling #humanize on that. Humanize will capitalize the first letter of the sentence and any defined acronyms.
If possible, I would store the strings as "IT professional", "DB administrator", etc. with all letters except for the acronyms already downcased. Then you can add your acronyms to the inflector and use #titleize to convert to title case when needed. In terms of edge cases and code maintenance burden, this is a better solution than writing your own code to do "selective downcasing".
If we assume that by acronym, you mean any word in your string that is made of 2 or more capitals in a row, then you could do something like this:
def smart_case(field)
field.to_s.split(' ').map { |word|
/[A-Z][A-Z]+/.match(word) ? word : word.downcase
}.join(' ')
end
This is an ugly way to do it but:
def format_me(str)
str.downcase!
#acronymn_words = ["IT Professional", "DB Administrator"]
#acronymn_words.each do |a|
if str.include? a.downcase
str.gsub!(a.downcase,a)
end
end
capitalize_next = true
str = str.split.map do |word|
if capitalize_next then word.capitalize! end
capitalize_next = word.end_with?(".","!","?")
word
end.join(" ")
end
This would be difficult to maintain unless you know the exact strings you are looking for but it will put out a correctly formatted sentence with the items you requested.
I would do this like that :
do_not_downcase = ["IT", "DB"] # Complete the list with your favourites words
res = ""
str.split(" ").each do |word|
if !do_not_downcase.include? word then
res += word.downcase + " "
else
res += word + " "
end
end
puts res
>welcome IT professionals