FoxPro Deleting after specific - visual-foxpro

I am trying to replace text after "/" on foxpro. Shelly Jones/Foundation Director, How to delete everything after the "/"?

A common way of doing it is to combine the left() and atc() functions, like so:
lcStr = "Shelly Jones/Foundation Director"
lcNewStr = LEFT(lcStr, ATC('/', lcStr) - 1)
The -1 is needed to get the portion of the string ending before the / character.

Assuming this is VFP 7 or later, use the StrExtract function:
cResult = STREXTRACT(cOriginalString, '', '/')

Related

Remove everything from trailing forward slash

Starting with this string:
file = "[/my/directory/file-*.log]"
I want to remove anything that comes after the trailing / and the closing square bracket, so the returned string is:
file = "[/my/directory/]"
I wondered if someone could recommend the safest way to do this.
I have been experimenting with chomp but I’m not getting anywhere, and gsub or sub doesn’t seem to fit either.
You can use File.dirname:
File.dirname("/my/directory/file-*.log")
=> "/my/directory"
If it's stuck inside the brackets, you can always write a custom replacement function that calls out to File.dirname:
def squaredir(file)
file.sub(/\[([^]]+)\]/) do |m|
'[%s]' % File.dirname($1)
end
end
Then you get this:
squaredir("[/my/directory/file-*.log]")
# => "[/my/directory]"
Here are three non-regex solutions:
file[0..file.rindex('/')] << ']'
file.sub(file[file.rindex('/')+1..-2], '')
"[#{File.dirname(file[1..-2])}]"
All return "[/my/directory/]".
file.split('/').take(file.split('/').size - 1).join('/') + ']'
=> "[/my/directory]"
Split the string into an array of strings separated by /
Take all the array elements except the last element
Join the strings together, re-inserting / between them
Add a trailing ]
file = "[/my/directory/file-*.log]"
file.sub(/^(.+)\/([^\/]+)\]/, '\1/]')
=> "[/my/directory/]"

Trimming a String

I'm using windows 7 and Visual C++. I have a console program and I am trying to trim a string at the begining and the end. TrimLeft() and TrimRight() don't seem to work without MFC. Here is what I have so far.
pBrowser->get_LocationURL(&bstr);
wprintf(L" URL: %s\n\n", bstr);
SysFreeString(bstr);
std::wstring s;
s = bstr;
s.TrimStart("http://");
s.TrimEnd("/*");
wprintf(L" URL: %s\n\n", s);
I'm trying to go from this:
"http://www.stackoverflow.com/questions/ask"
to this:
"www.stackoverflow.com"
TrimStart/End usually return a value, so you would have to set 's' to equal the value of s.TrimStart() and s.TrimEnd() respectively.
try,
s = s.TrimStart("http://");
s = s.TrimEnd("/*");
You should use find/rfind(right find - find from right) and substr(sub string) in sequence to do what you need to do.
1) Find the index of the first pattern (such as http://) with find - you already know its length, add this to the start index as the origo of your trimmed string
2) Find the last index of the ending pattern with find
3) Create a substring from the origo to the end using substr
These methods are all in std::string

Regular expression problem with ruby

I have a regular expression to match filenames which look like this:
name - subname goes here v4 03.txt
name - subname long 03.txt
name - subname v4 #03.txt
I want to extract the name and subname, without any addintional data. I'm able to extract the data just fine, the problem that is giving me errors is the v4 part (it's a version marker which is a v and a digit after it and it's not included everywhere), I want to exclude it but it extracts it along with the subname...
My regex looks like this:
^([\w \.]+)(?:-)?([\w \.-]+)? #?\d+
I tried doing something like this, but it only works without the ? at the end of "(?:v\d+ )?", and then it can't match filenames without the version:
^([\w \.]+)(?:-)?([\w \.-]+)? (?:v\d+ )?#?\d+
How do I make it work?
try this:
/^([\w \.]+?) - ([\w \.-]+?)(?: v\d+)? #?\d+/
I think you need to understand what is the difference of (\w+?) and (\w+)?
I would do this in two stages, first remove the parts that you don't want
a = str.sub /\s* (?: v\d+)? \s* \d+ \.[^.]*? $/x, ''
And then split the string on ' - '
a.split /\s*-\s*/

Ruby: Escaping special characters in a string

I am trying to write a method that is the same as mysqli_real_escape_string in PHP. It takes a string and escapes any 'dangerous' characters. I have looked for a method that will do this for me but I cannot find one. So I am trying to write one on my own.
This is what I have so far (I tested the pattern at Rubular.com and it worked):
# Finds the following characters and escapes them by preceding them with a backslash. Characters: ' " . * / \ -
def escape_characters_in_string(string)
pattern = %r{ (\'|\"|\.|\*|\/|\-|\\) }
string.gsub(pattern, '\\\0') # <-- Trying to take the currently found match and add a \ before it I have no idea how to do that).
end
And I am using start_string as the string I want to change, and correct_string as what I want start_string to turn into:
start_string = %("My" 'name' *is* -john- .doe. /ok?/ C:\\Drive)
correct_string = %(\"My\" \'name\' \*is\* \-john\- \.doe\. \/ok?\/ C:\\\\Drive)
Can somebody try and help me determine why I am not getting my desired output (correct_string) or tell me where I can find a method that does this, or even better tell me both? Thanks a lot!
Your pattern isn't defined correctly in your example. This is as close as I can get to your desired output.
Output
"\\\"My\\\" \\'name\\' \\*is\\* \\-john\\- \\.doe\\. \\/ok?\\/ C:\\\\Drive"
It's going to take some tweaking on your part to get it 100% but at least you can see your pattern in action now.
def self.escape_characters_in_string(string)
pattern = /(\'|\"|\.|\*|\/|\-|\\)/
string.gsub(pattern){|match|"\\" + match} # <-- Trying to take the currently found match and add a \ before it I have no idea how to do that).
end
I have changed above function like this:
def self.escape_characters_in_string(string)
pattern = /(\'|\"|\.|\*|\/|\-|\\|\)|\$|\+|\(|\^|\?|\!|\~|\`)/
string.gsub(pattern){|match|"\\" + match}
end
This is working great for regex
This should get you started:
print %("'*-.).gsub(/["'*.-]/){ |s| '\\' + s }
\"\'\*\-\.
Take a look at the ActiveRecord sanitization methods: http://api.rubyonrails.org/classes/ActiveRecord/Base.html#method-c-sanitize_sql_array
Take a look at escape_string / quote method in Mysql class here

Make space to dash with regexp

I've got a sinatra app where I plan to make a friedly-urls on the fly. I've got a function with a regexp that looks like this, but it won't turn 'spaces' into 'dashes', ' ' to '-'.
def self.make_slug(title)
title.downcase.gsub(/ /, '-').gsub(/[^a-z0-9_]/, '').squeeze('-')
end
Thanks in advance!
Update
Now I'm also trying to change åä into a and ö into o, my code looks like this but won't work, any ideas?
gsub(/[åä]/, 'a')
gsub(/[ö]/, 'o')
title.downcase.gsub(/\s+/, '-').gsub(/[^a-z0-9_-]/, '').squeeze('-')
This will take a given title (My & Title5:) and:
* downcase it (my & title5:)
* replace one or more white space characters with a - (my-&-title5:)
* replace non letters/number characters with nothing (my--title5)
* replace multiple occurrences of - with - (my-title5)
I hope this helps.
Whatever the language, you're first replacing " " with "-", and then replace everything but a-z0-9_ (thus, also "-") with "". Include "-" in the list like [^a-z0-9_-]

Resources