string.search(".") always return 0 - regex

I am work in Flash Builder 4.
Create e-mail validator on Flex.
Have this code
public var s:String="";
public function checkSumbols(_s:String=""):Boolean {
s=_s; //e-mail address (input mail#supermail.com)
var hDog:int=0;
var hPoint:int=0;
//check #
hDog=s.search("#");
trace(hDog) // It's work
if(hDog==-1) {
return false;
} else {
hPoint=s.substr(hDog).search(".");
trace(hPoint); // PANIC this return always 0
if(hPoint==-1){
return false;
}}
}

You could use regex. Since dot (.) has special meaning in regex you need to put 'escape' character before: yourString.search(/\./);
Should work.
HTH
FTQuest

search() accepts a pattern, and . just means "a single character", so it is probably returning the first single character, which would most likely be at index 0.
You could try search("\.")

I try with search(/[.]/) and it worked well in javascript, I think that It would work in the same mode in as3

Related

Evaluating a string against a pattern with RegExp in Flutter

I'm trying to evaluate a string against a set list of parameters with RegExp in Flutter. For example, the string must contain at least:
One capital letter
One lowercase letter
One number from 0-9
One special character, such as $ or !
This is basically for a password entry field of an application. I have set things up, firstly using validateStructure as follows:
abstract class PasswordValidator {
bool validateStructure(String value);
}
Then, I have used the RegExp function as follows:
class PasswordValidatorSpecial implements PasswordValidator {
bool validateStructure(String value) {
String pattern =
r'^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[!##\$&*~£]).{8,}$';
RegExp regEx = new RegExp(pattern);
return regEx.hasMatch(value);
}
}
This does work well, in a sense that when I pass a string/password through it, it does tell me if at least one of the criteria is not met. However, what I would like to do is for the output to be more specific, telling me which of those criteria isn't met.
For example, if the password were to have everything but a number (from 0-9) I would want to be able to get the output to specifically say that a number is missing, but everything else is present.
How would I adapt my code to be able to do that? I thought perhaps by using conditional 'if' statement, although I don't know how that would work. Thanks!
That's right, you can use RegExr to check your RegExp, separate each part and use them separately to have a custom error. Also instead of return a bool value, you can return a String value, such as the following function:
String validateStructure(String value) {
String patternUpperCaseCharacters = r'^(?=.*?[A-Z])';
String patternLowerCaseCharacters = r'^(?=.*?[a-z])';
String patternNumbers = r'^(?=.*?[0-9])';
String patternSpecialCharacters = r'^(?=.*?[!##\$&*~£])';
RegExp regEx = new RegExp(patternUpperCaseCharacters);
if (regEx.hasMatch(value)) {
regEx = new RegExp(patternLowerCaseCharacters);
if (regEx.hasMatch(value)) {
return "More errors";
} else {
return "You need at least one lowercase letter";
}
} else {
return "You need at least one capital letter";
}
}

how to exclude a string if it's in a URL using regex?

I'm replacing a number of different strings, but only want them to replace in normal text, and not get rewritten when they appear as a link in a document. The regex to find the strings is very straightforward: /word|anotherword|athirdword/gi but what that means is that if there's a link that contains anotherwordit's getting found and then replaced as well, breaking the link.
I think I just need a part in my regex that says "but just ignore anything that starts with http or https" but not sure how to write that.
thanks so much!
edit. here's what I'm doing with the javascript
if (node.nodeType === 3) {
var text = node.nodeValue;
var replacedText = text.replace(/word|anotherword|athirdword/gi, 'replaced text');
if (replacedText !== text) {
element.replaceChild(document.createTextNode(replacedText), node);
}
}
the result replaces those three strings anywhere on a page, which is great. except it changes http://www.foo.com/the-whole-world into http://www.foo.com/the-whole-replaced text which obviously breaks the link.
I would try negative lookbehind.
Negative lookbehind differs greatly from flavor to flavor, so it won't work in different flavors.
For JavaScript, you can try following:
str.replace(/(http:[\/\.-a-z0-9]+)?(word|anotherword|athirdword)/gi, function($0, $1){
return $1 ? $0 : '';
});
Fiddle.
You can split the string first, then do a conditional replace:
function condReplace(str) {
var sentences = [];
var res = str.split(/(https?:\/\/[^\s]+)(?:\s+|$)/i);
res.forEach(function(entry) {
if (entry) {
if (entry.match(/^http?:\/\//i)) {
sentences.push(entry);
} else {
sentences.push(entry.replace(/word|anotherword|athirdword/g, "REPLACED"))
}
}
});
document.write(sentences.join(" "));
}
var str = "http://sometext.com/word.doc and This is a word normal text anotherword containing a anotherword another link http://www.foo.com/the-whole-word. This is a single word.";
condReplace(str);

Regex to filter strings

I need to filter strings based on two requirements
1) they must start with "city_date"
2) they should not have "metro" anywhere in the string.
This need to be done in just one check.
To start I know it should be like this but dont know hoe to eliminate strings with "metro"
string pattern = "city_date_"
Added: I need to use the regex for a SQL LIKE statement. hence i need it in a string.
Use a negative lookahead assertion (I don't know if this is supported in your regex lib)
string pattern = "^city_date(?!.*metro)"
I also added an anchor ^ at the start, that will match the start of the string.
The negative lookahead assertion (?!.*metro) will fail, if there is the string "metro" somewhere ahead.
Regular expressions are usually far more expensive than direct comparisons. If direct comparisons can easily express the requirements, use them. This problem doesn't need the overhead of a regular expression. Just write the code:
std::string str = /* whatever */
const std::string head = "city_date";
const std::string exclude = "metro";
if (str.compare(head, 0, head.size) == 0 && str.find(exclude) == std::string::npos) {
// process valid string
}
by using javascript
input="contains the string your matching"
var pattern=/^city_date/g;
if(pattern.test(input)) // to match city_data at the begining
{
var patt=/metro/g;
if(patt.test(input)) return "false";
else return input; //matched string without metro
}
else
return "false"; //unable to match city_data

Capitalize every word in actionScript using a regular expression

I'm trying to do initial caps in actionScript without loops but I'm stuck. I wanted to select the first letter or every word then apply uppercase on that letter. Well I got the selection part right, but at a dead end right now, any ideas? I was trying to do this without loops and cutting up strings.
// replaces with x since I can't figure out how to replace with
// the found result as uppercase
public function initialcaps():void
{
var pattern:RegExp=/\b[a-z]/g;
var myString:String="yes that is my dog dancing on the stage";
var nuString:String=myString.replace(pattern,"x");
trace(nuString);
}
You can also use this to avoid the compiler warnings.
myString.replace(pattern, function():String
{
return String(arguments[0]).toUpperCase();
});
Try to use a function that returns the uppercase letter:
myString.replace(pattern, function($0){return $0.toUpperCase();})
This works at least in JavaScript.
Just thought I'd throw them two cents in for strings that may be all caps
var pattern:RegExp = /\b[a-zA-Z]/g;
myString = myString.toLowerCase().replace(pattern, function($0){return $0.toUpperCase();});
This answer does not throw any kind of compiler errors under strict and I wanted it to be a little more robust, handling edge cases like hyphens (ignore them), underscores (treat them like spaces) and other special non-word characters such as slashes or dots.
It's really important to note the /g switch at the end of the regular expression. Without it, the rest of the function is pretty useless, because it will only address the first word, and not any subsequent ones.
for each ( var myText:String in ["this is your life", "Test-it", "this/that/the other thing", "welcome to the t.dot", "MC_special_button_04", "022s33FDs"] ){
var upperCaseEveryWord:String = myText.replace( /(\w)([-a-zA-Z0-9]*_?)/g, function( match:String, ... args ):String { return args[0].toUpperCase() + args[1] } );
trace( upperCaseEveryWord );
}
Output:
This Is Your Life
Test-it
This/That/The Other Thing
Welcome To The T.Dot
MC_Special_Button_04
022s33FDs
For the copy-and-paste artists, here's a ready-to-roll function:
public function upperCaseEveryWord( input:String ):String {
return input.replace( /(\w)([-a-zA-Z0-9]*_?)/g, function( match:String, ... args ):String { return args[0].toUpperCase() + args[1] } );
}

Regex Rejecting matches because of Instr

What's the easiest way to do an "instring" type function with a regex? For example, how could I reject a whole string because of the presence of a single character such as :? For example:
this - okay
there:is - not okay because of :
More practically, how can I match the following string:
//foo/bar/baz[1]/ns:foo2/#attr/text()
For any node test on the xpath that doesn't include a namespace?
(/)?(/)([^:/]+)
Will match the node tests but includes the namespace prefix which makes it faulty.
I'm still not sure whether you just wanted to detect if the Xpath contains a namespace, or whether you want to remove the references to the namespace. So here's some sample code (in C#) that does both.
class Program
{
static void Main(string[] args)
{
string withNamespace = #"//foo/ns2:bar/baz[1]/ns:foo2/#attr/text()";
string withoutNamespace = #"//foo/bar/baz[1]/foo2/#attr/text()";
ShowStuff(withNamespace);
ShowStuff(withoutNamespace);
}
static void ShowStuff(string input)
{
Console.WriteLine("'{0}' does {1}contain namespaces", input, ContainsNamespace(input) ? "" : "not ");
Console.WriteLine("'{0}' without namespaces is '{1}'", input, StripNamespaces(input));
}
static bool ContainsNamespace(string input)
{
// a namspace must start with a character, but can have characters and numbers
// from that point on.
return Regex.IsMatch(input, #"/?\w[\w\d]+:\w[\w\d]+/?");
}
static string StripNamespaces(string input)
{
return Regex.Replace(input, #"(/?)\w[\w\d]+:(\w[\w\d]+)(/?)", "$1$2$3");
}
}
Hope that helps! Good luck.
Match on :? I think the question isn't clear enough, because the answer is so obvious:
if(Regex.Match(":", input)) // reject
You might want \w which is a "word" character. From javadocs, it is defined as [a-zA-Z_0-9], so if you don't want underscores either, that may not work....
I dont know regex syntax very well but could you not do:
[any alpha numeric]\*:[any alphanumeric]\*
I think something like that should work no?
Yeah, my question was not very clear. Here's a solution but rather than a single pass with a regex, I use a split and perform iteration. It works as well but isn't as elegant:
string xpath = "//foo/bar/baz[1]/ns:foo2/#attr/text()";
string[] nodetests = xpath.Split( new char[] { '/' } );
for (int i = 0; i < nodetests.Length; i++)
{
if (nodetests[i].Length > 0 && Regex.IsMatch( nodetests[i], #"^(\w|\[|\])+$" ))
{
// does not have a ":", we can manipulate it.
}
}
xpath = String.Join( "/", nodetests );