I'm having to check data entry on an address field. The client does not want users to use terms like Rd. or Rd for road, ave or ave. for avenue etc. I have no problem with most of the terms. Where I have issues is with 'Ave' lets say. If I look for ' AVE ', that's fine but it will not pick up on ' AVE' at the end of the string and if I look for ' AVE' it will get a false positive on ' Avenue' since it will find ' Ave' within that string. Anyone have an idea of how I can go about this?
Thank you for any help.
Norst
Although the Q: is not language specific, here is how I'm going about this in JS:
//function to check address for rd rd. ave ave. st st. etc
function checkaddy() {
//array of items to look for
var watchfor = Array();
watchfor[0] = " RD";
watchfor[1] = " RD.";
watchfor[2] = " AVE ";
watchfor[3] = " AVE.";
watchfor[4] = " ST ";
watchfor[5] = " ST.";
watchfor[6] = " BLVD.";
watchfor[7] = " CRT ";
watchfor[8] = " CRT.";
watchfor[9] = " CRES ";
watchfor[10] = " CRES.";
watchfor[11] = " E ";
watchfor[12] = " E.";
watchfor[13] = " W ";
watchfor[14] = " W.";
watchfor[15] = " N ";
watchfor[16] = " N.";
watchfor[17] = " S ";
watchfor[18] = " S.";
watchfor[19] = " PKWY ";
watchfor[20] = " PKWY.";
watchfor[21] = " DR ";
watchfor[22] = " DR.";
//get what the user has in the address box
var addcheck = $("#address").val();
//upper case the address to check
addcheck = addcheck.toUpperCase();
//check to see if any of these terms are in our address
watchfor.forEach(function(i) {
if (addcheck.search(watchfor[i]) > 0 ) {
alert("Found One!");
}
});
}
Perhaps you need to look for word boundary character \b. Here are some Ruby examples:
irb(main):002:0> " Avenue" =~ / AVE\b/i
=> nil
irb(main):003:0> " Ave" =~ / AVE\b/i
=> 0
irb(main):005:0> " Ave" =~ /\bAVE\b/i
=> 1
irb(main):006:0> " Ave" =~ /\bAVE\b/i
Notice how " Avenue" doesn't match while " AVE" does match. Also notice how the '\b' behaves and we get 0 and 1 respectively.
There are other characters classes as well in regular expressions. So all you need to do is formulate correct REs for your problem set.
I hope that helps.
Why would your client insist on spelling out names? The United States Postal Service actually encourages abbreviations. Not only that, they prefer addresses to be in all uppercase letters and no more than 5 lines. Such specifications are what their automated sorters were built for. But I digress.
To actually answer your question, you may consider the following code. There was a mistake in your forEach declaration. You were using i as an index, but, in fact, the forEach function uses the whole entry of the array. I modified it below. Also, because we're using a string expression in the constructor for the RegExp, the \ in the \b has to be escaped, so we add two \'s inside the string. Because we using the \b construct for word boundaries, we don't need to add extra periods into the test array. I hope you find this helpful.
//array of items to look for
var watchfor = ['RD','AVE','ST','BLVD','CRT','CRES','E','W','N','S','PKWY','DR'];
//function to check address for rd rd. ave ave. st st. etc
function checkaddy(address) {
//check to see if any of these terms are in our address
watchfor.forEach(function(entry) {
var patt1 = RegExp('.*\\b' + entry + '\\b.*','gim');
if (patt1.test(address)) {
document.write("Found " + entry);
}
});
}
Related
I want to replace an array of characters with empty space except values inside double quote in a string.
Example
"India & China" relationship & future development
In the above example, I need to replace & but thats not inside any of the double quotes(""). The expected result should be
Result
"India & China" relationship future development
Other Examples of String
relationship & future development "India & China" // Output: relationship future development "India & China"
"relationship & future development India & China // Output: reflect the same input string as result string when the double quote is unclosed.
I have so far done the below logic to replace the characters in a string.
Code
string invalidchars = "()*&;<>";
Regex rx = new Regex("[" + invalidchars + "]", RegexOptions.CultureInvariant);
string srctxtaftrep = rx.Replace(InvalidTxt, " ");
RegexOptions options = RegexOptions.None;
Regex regex = new Regex(#"[ ]{2,}", options);
srctxtaftrep = regex.Replace(srctxtaftrep, #" ");
InvalidTxt = srctxtaftrep;
Here's a non-regex approach using a StringBuilder which should work:
string input = "\"India & China\" relationship & future development";
HashSet<char> invalidchars = new HashSet<char>("()*&;<>");
StringBuilder sb = new StringBuilder();
bool inQuotes = false;
foreach(char c in input)
{
if(c == '"') inQuotes = !inQuotes;
if ((inQuotes && c != '"') || !invalidchars.Contains(c))
sb.Append(c);
}
string output = sb.ToString();
This is my code
for (int m=0; m < i ; m++){
ta1.setText( s[m].getName().toString() + ", " + s[m].getProgramName().toString() + ", " + s[m].getUni1() + ", " + s[m].getUni2() + ", " + s[m].getUni3() + ", " );
}
It's supposed to print a line from an array of student ( called s) into a JTextArea ( called ta1 ). the problem is that it always only prints the last student in the array.
I need to print each student in a new line. could anyone help me sort it out?
When you set text on an element, the current position in the loop will take over the last one.
Try doing this.
String s = "";
for(int m = 0, m <i; m++){
s += s[m].getName.toString() + ", " + s[m].getprogramName().toString() + "\n;
}
ta1.setText(s);
Create a string and add each entry to it then add new line to end of each entry "\n"
Then do.
ta1.setText(s);
setText overwrites whatever is the current text.
You need append instead; you also need a "\n" at the end of a line.
My professor is teaching us Scala using Horstmann's book "Scala for the impatient", and one of our homework exercises are straight from the book; Chapter 4, exercise 2.
We are expected to read in the eBook in text format, the professor has specified that the input file should be "Moby Dick", available for free from the Guttenberg project here: http://www.gutenberg.org/ebooks/2701.txt.utf-8
My code works, as far as counting instances of words. However, he has added the requirement that we must we must format the output in two two columns, with words left justified, and counts right justified. To do so, I am determining the longest word in the book so I can figure the width of the "word" column. However, the values I am getting for the length of the strings is just wrong. In fact, it tells me that all the strings are the same length. "a" is being reported as length 26, just as is "Whale", "Ishmael", etc...
Here's the code:
object Chapter4Exercise2 extends App {
//for sorting
import util.Sorting._
//grab the file
val inputFile = new java.util.Scanner(new java.io.File("moby.txt"))
//create a mutable map where key/values == word/count
val wordMap = collection.mutable.Map[String, Int]() withDefault (_ => 0)
//for formatting output (later), the longest word length is relevant
var longestWord = 0
var theWord: String = ""
//start reading each word in the input file
while (inputFile hasNext) {
//grab the next word for processing, convert it to lower case, trim spaces and punctuation
var nextWord = inputFile.next().toLowerCase().trim().filter(Character.isLetter(_))
//if it's the longest word, update both theWord and longestWord
if (nextWord.size > longestWord) longestWord = nextWord.size; theWord = nextWord; println(theWord + " " + longestWord)
//update the map value for the key with same value as nextWord
wordMap(nextWord) += 1
}
println("Longest word is " + theWord + " at " + longestWord + " Characters")
}
The output of these lines:
if (nextWord.size > longestWord) longestWord = nextWord.size; theWord = nextWord; println(theWord + " " + longestWord)
and
println("Longest word is " + theWord + " at " + longestWord + " Characters")
is way off. It's telling me that EVERY word in the input file is 26 characters long!
Here's a small sample of what's being output:
husks 26
on 26
a 26
surfbeaten 26
beach 26
and 26
then 26
diving 26
down 26
into 26
What am I missing/doing wrong?
if (nextWord.size > longestWord) longestWord = nextWord.size; theWord = nextWord; println(theWord + " " + longestWord)
You shouldn't write multiple statements on a single line like that. Let's write this out in multiple lines and properly indent it:
if (nextWord.size > longestWord)
longestWord = nextWord.size
theWord = nextWord
println(theWord + " " + longestWord)
Do you see the problem now?
Try putting { and } around your if statement alternatives.
You can avoid this kind of pitfall by formatting your code in a structured manner - always using braces around code blocks.
if (nextWord.size > longestWord)
{
longestWord = nextWord.size;
theWord = nextWord;
println(theWord + " " + longestWord);
}
Your current code is equivalent to
if (nextWord.size > longestWord)
{
longestWord = nextWord.size;
}
theWord = nextWord;
println(theWord + " " + longestWord);
I have a question regarding SharePoint permission masks. In SharePoint it is possible to set the grant/deny rights using masks. Details are given the following article.
http://msdn.microsoft.com/en-us/library/dd304243(PROT.13).aspx
My question is when we have a permission/deny mask.
For example if you deny “ViewItem” permission using the central-admin, you will get 4611686844973976575 as the deny mask. This permission masks is computed by aping | to several individual permission masks.
So is it possible to extract individual permission masks which are used to calculate permission mask such as 4611686844973976575?
Thanks.
If you do a logical AND operation on a value such as 0x0000000000000001 for "ViewListItems" which is contained in the mask, then you will get the value itself (or 1). If you do a logical AND on a value not in that mask, like the "UseClientIntegration" value of 0x0000001000000000, then you will get a zero (0). This is something you can even test via the scientific mode of the Windows calculator app -- perhaps first converting the mask to hex, such as taking your example of 4611686844973976575 from base 10 to 400000C072040BFF in hex (base 16).
To extract all values from the mask, you would have to test the initial value against all possible values. If all known permission values are documented on that page, then the answer to your question is yes. I don't know which language you may want to accomplish this, but the basic idea in C# is:
bool CheckMask( long Mask, long TestPermission ) {
return (Mask && TestPermission) > 0;
}
long mask = 4611686844973976575;
const long ViewListItems = 0x0000000000000001;
bool HasPermission_ViewListItems = CheckMask(mask, ViewListItems);
// HasPermission_ViewListItems is true
const long UseClientIntegration = 0x0000001000000000;
bool HasPermission_UseClientIntegration = CheckMask(mask, UseClientIntegration);
// HasPermission_UseClientIntegration is false
I made this javascript sample thanks to #zanlok answer
I used JQuery, SPServices js (http://spservices.codeplex.com/)
and this link for the masks codes
http://msdn.microsoft.com/en-us/library/dd304243%28PROT.13%29.aspx
I Hope this helps you, I did this because I was needing it also, however it may also help others.
You need to replace the divid with the value of the control you want to place the html, and the LIST NAME HERE with the name of the list.
The script will spit everyone that has access to a list, and say if they can read, add, change and delete things. Hopes this helps you.
$('#divid').html('Working...').SPServices({
operation: "GetPermissionCollection",
objectName: 'LIST NAME HERE',
objectType: "List",
completefunc: function (xData, Status) {
var out = "<ul>";
$(xData.responseXML).find("Permission").each(function () {
if ($(this).attr("MemberIsUser") === "True") {
out += "<li>User: " + $(this).attr("UserLogin") + "</li>";
} else {
out += "<li>Group: " + $(this).attr("GroupName") + "</li>";
}
var readmask = 0x0000000000000001;
var addmask = 0x0000000000000002;
var editmask = 0x0000000000000004;
var deletemask = 0x0000000000000008;
out += "<li>Mask: " + $(this).attr("Mask") + "</li>";
var canread = readmask & $(this).attr("Mask").toString(16) > 0 ? "Yes" : "No";
var canadd = addmask & $(this).attr("Mask").toString(16) > 0 ? "Yes" : "No";
var canedit = editmask & $(this).attr("Mask").toString(16) > 0 ? "Yes" : "No";
var candelete = deletemask & $(this).attr("Mask").toString(16) > 0 ? "Yes" : "No";
out += "<li>Can Read: " + canread + "</li>";
out += "<li>Can Add: " + canadd + "</li>";
out += "<li>Can Edit: " + canedit + "</li>";
out += "<li>Can Delete: " + candelete + "</li>";
});
out += "</ul>";
$('divid').html(out);
}
});
I would like to split byte strings, for example AAFF10DC, with spaces, so it becomes AA FF 10 DC.
How to do this in AutoIt (v3)?
I would like to split byte strings … with spaces …
Example using StringRegExpReplace() :
Global Const $g_sString = 'AAFF10DC'
Global Const $g_sPattern = '(.{2})'
Global Const $g_sReplace = '$1 '
Global Const $g_sResult = StringRegExpReplace($g_sString, $g_sPattern, $g_sReplace)
ConsoleWrite($g_sResult & #CRLF)
Returns AA FF 10 DC.
This is sorta ugly, but it works:
$string = "AAFF10DC"
$strArray = StringSplit($string, "") ; No delimiter will separate all chars.
$strResult = ""
If IsEvenNumber($strArray[0]) Then
For $i = 1 to $strArray[0] Step 2
$strResult = $strResult & $strArray[$i] & $strArray[$i+1] & " "
Next
MsgBox(0, "Result", $strResult)
Else
MsgBox(0, "Result", "String does not contain an even number of characters.")
EndIf
Func IsEvenNumber($num)
Return Mod($num, 2) = 0
EndFunc
Global $s_string = "AAFF10DC"
MsgBox(64, "Info", _str_bytesep($s_string))
Func _str_bytesep($s_str, $s_delim = " ")
If Not (Mod(StringLen($s_str), 2) = 0) Then Return SetError(1, 0, "")
Return StringRegExpReplace($s_str, "(..(?!\z))", "$1" & $s_delim & "")
EndFunc
Is just another way to do it. For huge amounts of byte data, I would not suggest using this method.