How do I use vbscript to find two texts that match within a singe line? For example:
This UserName is Logged on already.
How do I search for "UserName" and "Logged on"?
Regular expressions are probably overkill in this case. I'd suggest using InStr() for this kind of check:
s = "This UserName is Logged on already."
If InStr(s, "UserName") > 0 And InStr(s, "Logged on") > 0 Then
'...
End If
You can wrap InStr() in a helper function if you want to make the check a bit better readable:
s = "This UserName is Logged on already."
If Contains(s, "UserName") And Contains(s, "Logged on") Then
'...
End If
Function Contains(str1, str2)
Contains = False
If InStr(str1, str2) > 0 Then Contains = True
End Function
Related
I need to grant a claim to everyone not matching a particular LDAP attribute. I want to use a regex with a negative look ahead to perform this "not" clause
c1:[Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", Value =~ "^(?!Test User).*$"]
=> issue(Type = "http://goofyclaim", Value = "youre not a tester");
the above rule doesn't seem to get satisfied by my test users. Something wrong with the regex? or does ADFS4.0 not support it. I don't see any errors in the ADFS event logs.
this is a win2016srv on a win2012r2 AD domain.
for reference, this rule does work:
c1:[Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", Value =~ "(?i)^Test User1"]
=> issue(Type = "http://somethignelseentreily", Value = "imispellwhendriving");
first I need to use (found here ADFS rules language terminals) for REGEXP_NOT_MATCH
!~
Next, I had to restructure the regex mode modifier a little, by having the case insensitivity inside the ^ idenifier
c1:[Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", Value !~ "^(?i)Test User"]
=> issue(Type = "http://somethignelseentreily", Value = "imispellwhendriving");
(leaving my other answer so other can see its not the right answer)
NOT EXISTS([Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", Value =~ "^Test User"])
=> issue(Type = "http://somethignelseentreily", Value = "all");
I have variable with "origin/blahbranch" that I want to substring into "blahbranch", how to substring it? I tried with
dev newbranch = (branch1 =~ /.*)[0]
but that left me with
1. / sign included which I don't want
2. the actual git instruction returns error message when embedding the parameter ${newbranch} :
"unexpected char: '''"
Assuming branch1 is string you can use split function
List<String> list = new ArrayList<String>(Arrays.asList(branch1.split("/")));
list.remove(0);
def newbranch = String.join("/", list.toArray(new String[0]))
println newbranch
Very simple solution considering remote always remains origin you can do below
def newbranch = "origin/blahbrachwithslash/blahbranch".replace("origin/","")
println newbranch
I need to send a custom email message to every User of a list ( List < User > ) I have. (I'm using C# .NET)
What I would need to do is to replace all the expressions (that start with "[?&=" have "variableName" in the middle and then ends with "]") with the actual User property value.
So for example if I have a text like this:
"Hello, [?&=Name]. A gift will be sent to [?&=Address], [?&=Zipcode], [?&=Country].
If [?&=Email] is not your email address, please contact us."
I would like to get this for the user:
"Hello, Mary. A gift will be sent to Boulevard Spain 918, 11300, Uruguay.
If marytech#gmail.com is not your email address, please contact us."
Is there a practical and clean way to do this with Regex?
This is a good place to apply regex.
The regular expression you want looks like this /\[\?&=(\w*)\]/ example
You will need to do a replace on the input string using a method that allows you to use a custom function for replacement values. Then inside that function use the first capture value as the Key so to say and pull the correct corresponding value.
Since you did not specify what language you are using I will be nice and give you an example in C# and JS that I made for my own projects just recently.
Pseudo-Code
Loop through matches
Key is in first capture group
Check if replacements dict/obj/db/... has value for the Key
if Yes, return Value
else return ""
C#
email = Regex.Replace(email, #"\[\?&=(\w*)\]",
match => //match contains a Key & Replacements dict has value for that key
match?.Groups[1].Value != null
&& replacements.ContainsKey(match.Groups[1].Value)
? replacements[match.Groups[1].Value]
: "");
JS
var content = text.replace(/\[\?&=(\w*)\]/g,
function (match, p1) {
return replacements[p1] || "";
});
I am using the following REGEX
VALID_WEBSITE_REGEX = /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,6}(:[0-9]{1,5})?(\/.*)?$/ix
to validate a website entry with this rule:
validates :website, length: { maximum: 150 }, format: { with: VALID_WEBSITE_REGEX }
(The 150 is arbitrary).
However, when I save / update the form I get a validation error "website is invalid". How do I ensure that the 'format' section of the validation rule is processed only if there is content to process?
You can use allow_blank option for validation
:allow_blank => true
This option will let validation pass if the attribute’s value is blank?, like nil or an empty string for example.
read more:
http://guides.rubyonrails.org/active_record_validations_callbacks.html#allow_blank
Enclose the entire thing with the ? operator, e.g.
VALID_WEBSITE_REGEX = /^((http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,6}(:[0-9]{1,5})?(\/.*)?)?$/ix
If you want to allow whitespace too, then add \s* on each end, e.g.
VALID_WEBSITE_REGEX = /^\s*((http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,6}(:[0-9]{1,5})?(\/.*)?)?\s*$/ix
I've tried implementing a rather simple email validation function that seems return a false match even though the input is a valid email. I have searched for any issues with the existing regex but it seems to be correct.
Even though the match returns a false value the program is stepping to the next validation level (which it shouldn't).
Here is the email validation function.
Function EmailAddressChecker(ByVal emailAddress As String) As Boolean
Dim regExPattern As String = "^[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$"
Dim emailAddressMatch As Match = Regex.Match(emailAddress, regExPattern)
If emailAddressMatch.Success Then
Return True
Else
Return False
End If
End Function
And for the form validation that calls upon the email validation function.
If (String.IsNullOrEmpty(EmailTextBox.Text) OrElse EmailAddressChecker(EmailTextBox.ToString)) Then
MessageBox.Show("Please enter a valid email addresss")
Return False
End If
The call for all of this happens on an click event which triggers a cascading serious of If statements checking to see if all the fields are set.
Skipping a large chunk of code the click event asks if "AreFieldsSet <> True". Inside of the "AreFieldsSet" function contains all the validation for multiple inputs; one of which is the email validation if statement.
Are the emails in UpperCase? If they aren't, they won't match.
If you want to modify the Regex so that it is Case insensitive, use this:
"^[a-zA-Z0-9._%+-]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$"
To validate the email address you need to use the IsMatch function of the Regex object, it evaluate if the entry email address is valid.
Function EmailAddressChecker(ByVal emailAddress As String) As Boolean
Dim r As System.Text.RegularExpressions.Regex = Nothing
Dim regExPattern As String = "^[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$"
If r.IsMatch(emailAddress ,regExPattern ) Then
Return True
Else
Return False
End If
End Function
You can try this code for your form validation If (String.IsNullOrEmpty(EmailTextBox.Text) OrElse EmailAddressChecker(EmailTextBox.ToString)<>true) Then
MessageBox.Show("Please enter a valid email addresss")
Return False
End If
Public Shared Function ValidEmailAddress(ByVal emailAddress As String, ByRef errorMessage As String) As Boolean
If emailAddress.IndexOf("#") > -1 Then
If (emailAddress.IndexOf(".", emailAddress.IndexOf("#")) > emailAddress.IndexOf("#")) AndAlso emailAddress.Split(".").Length > 0 AndAlso emailAddress.Split(".")(1) <> "" Then
errorMessage = ""
Return True
End If
End If
Return False
End Function