Hi i was copying a code form the net,for a program but there where some isuees...
I understand everything , except this one ,please tell me everything about that...
".*\\d.*"
this is the hole code
public void checkS(String w){
this.w = w;
do{
if(w.matches(".*\\d.*")){
System.out.println("Contains a number,try again....(ya aint 2 Pac) ");
} else{
System.out.println("Allright "+">"+w+"<"+" lets go one and start the shit...");
runL=3;
}
}while(runL==2); }
and who do i write the thing between the [] for example if i want to search for :$"%&/ ?
Please give me a source to this topic i already search but didnt found something usefull
And if my english is bad pls Tell :D
The String.matches method performs a regular expression match. There are a number of good resources out there describing regular expressions. In this specific case ".*\\d.*" will match anything which contains at least 1 digit \d anywhere in the string.
The regular expression .*\\d.* does the following:
.*: Matches zero or more (*) of any character (.)
\\d: \d matches a number but you have to add an extra '\' because in java two back slashes (\\) becomes one. This is called escaping.
.*: same as mentioned previously
So all in all it will match any string that has zero or more characters one number then zero or more characters after that.
Some examples:
abasdfadsf7asdga
foo87980890bar
this is a str1ng that matches!
7
Related
I am trying to create a small program that reads a password and determines if it is a password you can use. I was using matches() to see if it has at least one letter (uppercase or lowercase) and one digit as well as be 6 characters long. I try to use matches like this:
if ( passwordLength >= 6
&& password.matches("[A-Za-z]")
&& password.matches("\\d")
) {
System.out.println("Valid Password.");
}
else {
System.out.println("Invalid Password.");
}
Im not sure what i am doing wrong. Please help.
matches function should try to match the whole string using the regex we gave. So instead of using two or more matches functions for condition checking, you may use a single one with a complex regex. And also it seems like your password would contain not only digits or letters but also some other characters too.
string.matches("^(?=.*[a-zA-Z])(?=.*\\d).{6,}$");
(?=.*[a-zA-Z]) Positive lookahead which asserts that the string going to be matched must contain atleast one letter.
(?=.*\\d) asserts it must contain atleast one digit.
.{6,} ensures that the length must be atleast 6 and atmost any..
For the length to be exactly 6 then change .{6,} in the above to .{6}
DEMO
It looks like you're misinterpreting the matches function, which matches over the entire input, while you're expecting it to return true if a substring matches. As others suggested, you'll need to use a single regex and matches() call since you're asking if the string is a single character AND a number (which would never be true).
Another important point, though it may not be important to you, is that a password should never be stored as a String object since they're immutable and can persist long enough for something else to come along and read it. More on that here.
There are a couple of things wrong with your code:
Your regex is wrong. You are only matching on 1 character, so you should change the [a-z] to [a-z]+.
You are matching on 'it should be bigger than 6, should be a character, AND it should be a number. That's different from your requirement.
I'm looking for a regex that will match a period character, ONLY if none of that period's surrounding characters are also periods.
Fine by me... leave! FAIL
Okay.. You win. SUCCEED
Okay. SUCCEED //Note here, the period is the last char in the string.
I was thinking do:
[^\\.*]\\.
But that is just wrong and probably not at all in the right direction. I hope this question helps others in the same situation as well.
Thanks.
You need to wrap the dot in negative look arounds:
(?<![.])[.](?![.])
I prefer [.] over \\., because:
It's easier to read - there are too many back slashes in java literals already
[.] looks a bit like an X wing fighter from Star Wars ™
You can use negative look ahead and look behind or this alternative regex:
String regex = "(^\\.[^\\.]|[^\\.]\\.[^\\.]|[^\\.]\\.$)";
The first alternative check the beginning ^ of the string (if it can start with a dot), the second looks for any dot inside and the third looks for a dot at the end of the string $.
That regex will still match any period that isn't preceded by another period.
[^\.]\.[^\.] Takes care of both sides of the target period.
EDIT: Java doesn't have a raw string like Python, so you would need full escapes: [^.]\\.[^.]|^\\.[^.]|[^.]\\.$
This is what i've been using
\$?[0-9]+\.*[0-9]*
But when i was doing some testing i noticed that things like
$$$34.00
would return as a match (but matcher.group()) just returns the matched substring. I don't want it to even pass the regular expression if the user enters more than one dollar sign so i tried this:
\${1}[0-9]+\.*[0-9]*
but this seems to behave the same as the regular expression i first typed. Right now i'm testing this in java but, i plan to use it in c++ using the Boost libraries. But Please don't give me that solution here because i'm trying to learn without someone giving me the answer.
But i do need help making it so the user can only enter one dollar sign (which is what i thought \${1} would do)
I would suggest avoiding the use of regular expressions for currency parsing, esp when Java provides you much simpler ways to solve this problem.
Consider this code:
String str = "$789.11"; // user entered value
Number number = null;
try {
number = NumberFormat.getCurrencyInstance(Locale.US).parse(str);
} catch(ParseException pe) {
// ignore
}
if (number != null) {
// proceed as user entered a good value
}
else {
// user didn't enter a good value
}
Since you're doing this to learn regex...
^\$(([1-9]\d{0,2}(,\d{3})*)|(([1-9]\d*)?\d))(\.\d\d)?$
Breakdown:
^\$ start of string with $ a single dollar sign
([1-9]\d{0,2}(,\d{3})*) 1-3 digits where the first digit is not a 0, followed by 0 or more occurrences of a comma with 3 digits
or
(([1-9]\d*)?\d) 1 or more digits where the first digit can be 0 only if it's the only digit
(\.\d\d)?$ with a period and 2 digits optionally at the end of the string
Matches:
$4,098.09
$4098.09
$0.35
$0
$380
Does not match:
$098.09
$0.9
$10,98.09
$10,980456
You can do something like [^\$]*\$? in the beginning. This would insure that there are no duplicate $ signs, but also matches if there is no $ present.
Also, if you are working with currency (possible decimal and 2 digits after), you should use [\.\d{2}]?.
This says that it can be a match if it's followed by ONE instance of a period and 2 digits or nothing at all. As stated in the comments, it can also match multiple periods in a row, so you shouldn't use the * quantifier after \.
You are missing ^(beginning of string),$(end of string)
^\$\d+([.][0-9]+)?$
i know this is coming late but this seems to work for me
(\$)?[0-9]+\.*[0-9]*
i dont understand why it didnt work for you.
[$](([1-9]+\.?\d*)|([0]\.\d*)|[0]) pattern ideally for this case.
This is standard Perl regex but could be use in JAVA with some libs.
I need a regular expression for below pattern
It can start with / or number
It can only contain numbers, no text
Numbers can have space in between them.
It can contain /*, at least 1 number and space or numbers and /*
Valid Strings:
3232////33 43/323//
3232////3343/323//
/3232////343/323//
Invalid Strings:
/sas/3232/////dsds/
/ /34343///// /////
///////////
My Problem is, it can have space between numbers like /3232 323/ but not / /.
How to validate it ?
I have tried so far:
(\\d[\\d ]*/+) , (/*\\d[\\d ]*/+) , (/*)(\\d*)(/*)
This regex should work for you:
^/*(?:\\d(?: \\d)*/*)+$
Live Demo: http://www.rubular.com/r/pUOYFwV8SQ
My solution is not so simple but it works
^(((\d[\d ]*\d)|\d)|/)*((\d[\d ]*\d)|\d)(((\d[\d ]*\d)|\d)|/)*$
Just use lookarounds for the last criteria.
^(?=.*?\\d)([\\d/]*(?:/ ?(?!/)|\\d ?))+$
The best would have been to use conditional regex, but I think Java doesn't support them.
Explanation:
Basically, numbers or slashes, followed by one number and a space, or one slash and a space which is not followed by another slash. Repeat that. The space is made optional because I assume there's none at the end of your string.
Try this java regex
/*(\\d[\\d ]*(?<=\\d)/+)+
It meets all your criteria.
Although you didn't specifically state it, I have assumed that a space may not appear as the first or last character for a number (ie spaces must be between numbers)
"(?![A-z])(?=.*[0-9].*)(?!.*/ /.*)[0-9/ ]{2,}(?![A-z])"
this will match what you want but keep in mind it will also match this
/3232///// from /sas/3232/////dsds/
this is because part of the invalid string is correct
if you reading line by line then match the ^ $ and if you are reading an entire block of text then search for \r\n around the regex above to match each new line
I need to validate input string which should be in the below format:
<2_upper_case_letters><"-"><2_upper_case_letters><14-digit number><1_uppercase_letter>
Ex: RX-EZ12345678912345B
I tried something like this ^[IN]-?[A-Z]{0,2}?\\d{0,14}[A-Z]{0,1} but its not giving the expected result.
Any help will be appreciated.
Thanks
Your biggest problem is the [IN] at the beginning, which matches only one letter, and only if it's I or N. If you want to match two of any letters, use [A-Z]{2}.
Once you fix that, your regex will still only match RX-E. That's because [A-Z]{0,2}? starts out trying to consume nothing, thanks to the reluctant quantifier, {0,2}?. Then \d{0,14} matches zero digits, and [A-Z]{0,1} greedily consumes the E.
If you want to match exactly 2 letters and 14 digits, use [A-Z]{2} and \d{14}. And since you're validating the string, you should end the regex with the end anchor, $. Result:
^[A-Z]{2}-[A-Z]{2}\d{14}[A-Z]$
...or, as a Java string literal:
"^[A-Z]{2}-[A-Z]{2}\\d{14}[A-Z]$"
As #nhahtdh observed, you don't really have to use the anchors if you're using Java's matches() method to apply the regex, but I recommend doing so anyway. It communicates your intent better, and it makes the regex portable, in case you have to use it in a different flavor/context.
EDIT: If the first two characters should be exactly IN, it would be
^IN-[A-Z]{2}\d{14}[A-Z]$
Simply translating your requirements into a java regex:
"^[A-Z]{2}-[A-Z]{2}\\d{14}[A-Z]$"
This will allow you to use:
if (!input.matches("^[A-Z]{2}-[A-Z]{2}\\d{14}[A-Z]$")) {
// do something because input is invalid
}
Not sure what you are trying to do at the beginning of your current regex.
"^[A-Z]{2}-[A-Z]{2}\\d{14}[A-Z]$"
The regex above will strictly match the input string as you specified. If you use matches function, ^ and $ may be omitted.
Since you want exact number of repetitions, you should specify it as {<number>} only. {<number>,<number>} is used for variable number of repetitions. And ? specify that the token before may or may not appear - if it must be there, then specifying ? is incorrect.
^[A-Z]{2}-[A-Z]{2}\\d{14}[A-Z]$
This should solve your purpose. You can confirm it from here
This should solve your problem. Check out the validity here
^[A-Z]{2}-[A-Z]{2}[0-9]{14}[A-Z]$
^([A-Z]{2,2}[-]{1,1}[A-Z]{2,2}[0-9]{14,14}[A-Z]{1,1}){1,1}$