How to check whether a character expression contains a "*" in the beginning of the text using the like function - visual-foxpro

cFilterprodname=upper(thisform.textbox1.text)
IF LIKE('*'+"%",cFilterprodname)=.T.
MESSAGEBOX("* found in the beginning")
ELSE
MESSAGEBOX("* not found in the beginning")
ENDIF
This is the code which I tried and it is jumping to else block even if it has '*' in the beginning. Can someone help me?

That's not how the LIKE function works. First, the '*' is a wildcard, matching any number of characters, so it's not going to match on the character.
Second, from the help file:
cExpression2 must match cExpression1 letter for letter in order for LIKE( ) to return true (.T.).
If you want to check if the first character is a '*', this would work and should be easier to understand.
IF LEFT(cFilterprodname, 1) == "*"
MESSAGEBOX("* found in the beginning")
ELSE
MESSAGEBOX("* not found in the beginning")
ENDIF

Related

What does the /^ symbol mean in Ruby?

A friend of mine is trying to explain to me the answer to this problem:
Define a method binary_multiple_of_4?(s) that takes a string and returns true if the string represents a binary number that is a multiple of 4.
However, his example he gave is this:
if (s) == "0"
return true
end
if /^[01]*(00)$/.match(s) #|| /^0$/.match(s)
return true
else
return false
end
It works, because the software we use says there were no errors, but I don't understand why, or what /^ means, and how it's used.
If you could also explain the /^0$/.match(s), that would be great too.
Thanks!
what he is doing is using regular expressions, see: http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm
To break it down, there is a pattern that is matched inside the slashes /pattern/ and every character means something. ^ means start of the line [01] means match a 0 or a 1, * means match the previous thing ([01]) zero or more times, and (00) means match 00, and $ means match the end of the line.
If you want to know what /^0$/ matches, you should definitely try to figure it out based on the information in my post or the link I provided. Here's the answer though (hover to view):
It matches the beginning of the line, zero, the end of a line.

Deleting all special characters from a string - ruby

I was doing the challenges from pythonchallenge writing code in ruby, specifically this one. It contains a really long string in page source with special characters. I was trying to find a way to delete them/check for the alphabetical chars.
I tried using scan method, but I think I might not use it properly. I also tried delete! like that:
a = "PAGE SOURCE CODE PASTED HERE"
a.delete! "!", "#" #and so on with special chars, does not work(?)
a
How can I do that?
Thanks
You can do this
a.gsub!(/[^0-9A-Za-z]/, '')
try with gsub
a.gsub!(/[!#%&"]/,'')
try the regexp on rubular.com
if you want something more general you can have a string with valid chars and remove what's not in there:
a.gsub!(/[^abcdefghijklmnopqrstuvwxyz ]/,'')
When you give multiple arguments to string#delete, it's the intersection of those arguments that is deleted. a.delete! "!", "#" deletes the intersections of the sets ! and # which means that nothing will be deleted and the method returns nil.
What you wanted to do is a.delete! "!#" with the characters to delete passed as a single string.
Since the challenge is asking to clean up the mess and find a message in it, I would go with a whitelist instead of deleting special characters. The delete method accepts ranges with - and negations with ^ (similar to a regex) so you can do something like this: a.delete! "^A-Za-z ".
You could also use regular expressions as shown by #arieljuod.
gsub is one of the most used Ruby methods in the wild.
specialname="Hello!#$#"
cleanedname = specialname.gsub(/[^a-zA-Z0-9\-]/,"")
I think a.gsub(/[^A-Za-z0-9 ]/, '') works better in this case. Otherwise, if you have a sentence, which typically should start with a capital letter, you will lose your capital letter. You would also lose any 1337 speak, or other possible crypts within the text.
Case in point:
phrase = "Joe can't tell between 'large' and large."
=> "Joe can't tell between 'large' and large."
phrase.gsub(/[^a-z ]/, '')
=> "oe cant tell between large and large"
phrase.gsub(/[^A-Za-z0-9 ]/, '')
=> "Joe cant tell between large and large"
phrase2 = "W3 a11 f10a7 d0wn h3r3!"
phrase2.gsub(/[^a-z ]/, '')
=> " a fa dwn hr"
phrase2.gsub(/[^A-Za-z0-9 ]/, '')
=> "W3 a11 f10a7 d0wn h3r3"
If you don't want to change the original string - i.e. to solve the challenge.
str.each_char do |letter|
if letter =~ /[a-z]/
p letter
end
end
You will have to write down your own string sanitize function, could easily use regex and the gsub method.
Atomic sample:
your_text.gsub!(/[!#\[;\]^%*\(\);\-_\/&\\|$\{#\}<>:`~"]/,'')
API sample:
Route: post 'api/sanitize_text', to: 'api#sanitize_text'
Controller:
def sanitize_text
return render_bad_request unless params[:text].present? && params[:text].present?
sanitized_text = params[:text].gsub!(/[!#\[;\]^%*\(\);\-_\/&\\|$\{#\}<>:`~"]/,'')
render_response( {safe_text: sanitized_text})
end
Then you call it
POST /api/sanitize_text?text=abcdefghijklmnopqrstuvwxyz123456<>$!#%23^%26*[]:;{}()`,.~'"\|/

Ruby regex: split string with match beginning with either a newline or the start of the string?

Here's my regular expression that I have for this. I'm in Ruby, which — if I'm not mistaken — uses POSIX regular expressions.
regex = /(?:\n^)(\*[\w+ ?]+\*)\n/
Here's my goal: I want to split a string with a regex that is *delimited by asterisks*, including those asterisks. However: I only want to split by the match if it is prefaced with a newline character (\n), or it's the start of the whole string. This is the string I'm working with.
"*Friday*\nDo not *break here*\n*But break here*\nBut again, not this"
My regular expression is not splitting properly at the *Friday* match, but it is splitting at the *But break here* match (it's also throwing in a here split). My issue is somewhere in the first group, I think: (?:\n^) — I know it's wrong, and I'm not entirely sure of the correct way to write it. Can someone shed some light? Here's my complete code.
regex = /(?:\n^)(\*[\w+ ?]+\*)\n/
str = "*Friday*\nDo not *break here*\n*But break here*\nBut again, not this"
str.split(regex)
Which results in this:
>>> ["*Friday*\nDo not *break here*", "*But break here*", "But again, not this"]
I want it to be this:
>>> ["*Friday*", "Do not *break here*", "*But break here*", "But again, not this"]
Edit #1: I've updated my regex and result. (2011/10/18 16:26 CST)
Edit #2: I've updated both again. (16:32 CST)
What if you just add a '\n' to the front of each string. That simplifies the processing quite a bit:
regex = /(?:\n)(\*[\w+ ?]+\*)\n/
str = "*Friday*\nDo not *break here*\n*But break here*\nBut again, not this"
res = ("\n"+str).split(regex)
res.shift if res[0] == ""
res
=> [ "*Friday*", "Do not *break here*",
"*But break here*", "But again, not this"]
We have to watch for the initial extra match but it's not too bad. I suspect someone can shorten this a bit.
Groups 1 & 2 of the regex below :
(?:\A|\\n)(\*.*?\*)|(?:\A|\\n)(.*?)(?=\\n|\Z)
Will give you your desired output. I am no ruby expert so you will have to create the list yourself :)
Why not just split at newlines? From your example, it looks that's what you're really trying to do.
str.split("\n")

Regexp, how to limit a match

I have a string:
string = %q{<span class="no">2503</span>read_attribute_before_type_cast(<span class="pc">self</span>.class.primary_key)}
In this example I want to match the words 'class' which are not in the tag. Regexp for this:
/\bclass[^=]/
But the problem is that it matches the last letter
/\bclass[^=]/.match(string) => 'class.'
I don't want have a last dot in a result. I've tried this regexp:
/\bclass(?:[^=])/
but still got the same result. How to limit the result to 'class'? Thanks
You are almost correct, but you have an error in your look ahead. Try this:
/\bclass(?!=)/
The regex term (?!=) means the input to the right must not match the character '='
You can take your variable string and extract a subsection using groups:
substring = string[/\b(class)[^=]/, 1]
The brackets around class will set that as the first "group", which is referred to by the 1 as the second parameter in the square brackets.
Assuming your only issue is keeping it from matching span.class.blah, just ignore . as well, so [^=.].

Help with regular expression

I have the following lines I have to match
A
Ab
A#
F#7+9d
G3+9d
Gm
Basically I need to match the first letter and the # or the b following. I also need to match anything else (such as the 7+9d or the m).
Here is my code so far but it's not picking up the second part
preg_match('/A-G([A-Z0-9+]?)/i', $start_key, $matches)
Any ideas?
Try this
/^A-G(#|b)?([A-Z0-9+]*)$/i
Try adding a-z and # to the body of the character set, converting your statement into:
preg_match('/A-G([A-Za-z0-9+#]?)/i', $start_key, $matches)

Resources