Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 days ago.
Improve this question
I need to generate a random password in Visual Basic that contains 8 characters including uppercase, lowercase, a special character and very important two consecutive numbers, i can generate the password with Membership.GeneratePassword(Int32, Int32) Method or another methods. But I need at least two consecutive numbers.
Private function GeneratePassword() As String
'Generate a new 8-character password with at least 1 non-alphanumeric character.
Dim password As String = Membership.GeneratePassword(8, 1)
Return password
End Function
This does not always give me the result that I expect it to be with two consecutive numbers.
for example.
ApLoc*25
could I generate a string and then generate two random numbers with a for and concatenate them? Wouldn't this be good practice?
If you require two consecutive numbers then I would generate them and append them as you suggested. If you'd like you can do some additional logic to insert them into your string at a random index as well. A GetRandom function is below. For simplicity, I've excluded <9 and >99 to avoid giving you single digits or more than two digits.
Public Function GetRandom(ByVal Min As Integer, ByVal Max As Integer) As Integer
Static Generator As System.Random = New System.Random()
Return Generator.Next(Min, Max)
End Function
Private function GeneratePassword() As String
'Generate a new 8-character password with at least 1 non-alphanumeric character.
Dim password As String = Membership.GeneratePassword(8, 1)
password.insert(GetRandom(0, password.Length()-1), CStr(GetRandom(10,99))
Return password
End Function
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
I am completely new to the world of Regular Expressions, and was wondering if someone could provide me with some assistance on getting an expression going.
In my scenario, I need to check if a string contains one letter, and the one letter can either be an A or B. Only phrases with a single letter as A or B are permitted.
Ideally the expression would identify the "Good" values as matches and reject the "Bad" values due to containing multiple letters and not a single A or B.
Any help would be very much appreciated
Thanks!
If you just need the result and regex is not mandatory, you could use a simple expression as below
bool result = word.Where(Char.IsLetter).Count() == 1 && (word.Contains('A') || word.Contains('B'));
The expression ^[^A-Z]*[AB][^A-Z]*$ matches a string containing exactly one letter that is either A or B.
Explanation:
^ Matches the start of the string.
[^A-Z] Matches any character that is not a lette
* Means zero or more of the previous item, thus
[^A-Z]* Matches zero or more characters that are not letters
[AB] Matches either an `A` or a `B`
[^A-Z]* Matches zero or more characters that are not letters
$ Matches the end of the string
If the string should contain at least one character before and after the A or B then the pattern should be modified to be ^[^A-Z]+[AB][^A-Z]+$. Using the + means matching be one or more of the previous item whereas the * means zero or more.
The pattern [A-Z] matches any letter. [^A-Z] matches any character that is not a letter. Similarly [AB] matches either an A or a B. [^AB] matches any character that is not A or B, but this pattern is not needed here. Putting these together gives t
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
For Example I have String like this:
""dear customer{Customer name} your reference number is {referenceNumber}"
I want to get array=["{Customer name}",{referenceNumber}]"
I have to split based on curly bracket inside bracket value is changeable means it can be different for different cases I just need to split and get array of value inside brackets including brackets.
If you think about it, splitting on { and } will produce an array where every odd index is what you want..
.Split('{','}').Where((s,i)=>i%2==1).Select(s=>'{' + s + '}').ToArray();
Split the string, use the LINQ Where function that passes the int index to the predicate, insist that the index be odd (mod2 is 1) and select a new string that puts the brackets back on, ToArray
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have this regex example. I'm new to regex.
^[a-z0-9](\.?[a-z0-9_-]){0,}#[a-z0-9-]+\.([a-z]{1,6}\.)?[a-z]{2,6}$
I'm thinking where is the top level domain here, is it starting on the right or on the left? And what is it?
The regex you've provided won't allow Numbers in top level domains.
[a-z]{2,6}$ is the part checking the top level domain. It'll only allow lower case characters, minimal 2 characters max 6 characters.
EDIT: Let's deconstrunct your your regex for clearity.
^[a-z0-9](\.?[a-z0-9_-]){0,}#[a-z0-9-]+\.([a-z]{1,6}\.)?[a-z]{2,6}$
the ^[a-z0-9](\.?[a-z0-9_-]){0,} part checks from the beginning of the string if there are only allowed characters that are a-zand 0-9 followed by an optional . followed by a-zand 0-9 aswell as _ and - zero to infinite times.
#[a-z0-9-]+\.([a-z]{1,6}\.)? checks for an # after the first part aswell as validity of the domain, a-z and 0-9 including - followed by a . with an optional a-z2 to 6 times, up to one time i.E. google.com would be acceptable at this point but the .com part is optional.
[a-z]{2,6}$ checks for a-z 2 to 6 times and the end of the string indicated by $. meaning this is the part checking for the top level domain.
your regex would accept: blah#google.com aswell as blah#google.com.com
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to store some string code in SQL like 120.002.123 or EXP.120.555. How can I split those strings to increase only the rightmost integer part? I don't want to change other parts of the string.
Ex. 120.002.124 or EXP.120.556
How can I do this in C#?
This might do the trick for you
string str = "EXP.120.556"; //120.002.123
str = String.Join(
".",
str.Split('.')
.Take(
str.Split('.').Length - 1
)
)
+ "." +
(
Convert.ToInt32
(
str.Split('.').LastOrDefault()
) + 1
).ToString();
So here in the code the first String.Join will join the other part of the string with . except of the last part which you want to increament using Take. Then Convert.ToInt32 will convert the last part of the string to a integer using LastOrDefault and then we add 1 to it and again convert it back to string using ToString
What you can do is to use String.Split method and split by . to get each individual number.
string test = "120.002.123";
string[] allparts = test.Split('.');
Then take the last entry and convert it to an integer
int lastNumber = Convert.ToInt32(allparts.Last());
Now you can do what ever you want with that integer.
With a little more research effort you could have solved it on your own.
Like this + this
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I don't know if you got me what I'm trying to explain... (English is not my main language).
Ok here, I got this string 590CBC145FA and this one F6EC5CA9A and the only part that I want, no matter how long is the string, is the last eight char like this: CBC145FA 6EC5CA9A. The 590 and F are obsolete to me, like: !S!ZV)+D_?CEFZEZAF = CEFZ!Z#F.
I tried to use serial.Substring(3); only work for the first one: 590CBC145FA = CBC145FA
If I try to use serial.Substring(1); it work for F6EC5CA9A = 6EC5CA9A
But what happen if I get a string very long and I don't know how much the obsolete part is... I can't use serial.Substring(X); because I don't, like I said, how long is the obsolete part.
I only want to get the eight last char of any serial no matter how long is.
You want to get the eight last char of any serial no matter how long is.
string test = "F6EC5CA9A";
string result = test;
if (test.Length >= 8)
result = test.Substring(test.Length-8);
You just need to discover the index of the 8th character before the end of the string. And this could be easily calculated using the Length property of every string.
Of course you need to be sure that your string is at least 8 characters. You don't say what do you want in case this length is less than 8, so I assume that you want the original input back