In my application edittext value need at least one digit and one alphabet is mandatory, and some special characters are optional i.e ".-", like any whare in the string.
For example ram123-. or r_m-12.m or .--ram123 or ram123.-_.
For this I need regex. I have already tried with this one
str_userId.matches("[A-Za-z0-9]*+[?.?_?-]")
But not working. Here how to add special characters are optional.
Thanks, In Advance
You could use a positive lookahead (?= to assert at least one occurrence of a-z and after that match at least a single digit [0-9].
Before and after matching the digit, you could add the . _ and - to the character class [A-Za-z._-]* and repeat it 0+ times.
Note that a character class matches on of the listed characters. This notation [?.?_?-], which could be written as [?._-] would also match a question mark instead of making the others optional
^(?=[^a-z\n]*[a-z])[A-Za-z._-]*[0-9][A-Za-z0-9._-]*$
Regex demo
Related
I need help with creating a regex that removes all special characters, including commas, but not periods. What I have tried to do is escape all the characters, symbols and punctuation I do not want. It is not working as intended.
replace("[-\\[\\]^/,'*:.!><~##\$%+=?|\"\\\\()]+".toRegex(), "")
I removed the period and tested that too. It did not work.
replace("[-\\[\\]^/,'*:!><~##\$%+=?|\"\\\\()]+".toRegex(), "")
For example, lets take the String "if {cat.is} in a hat, then I eat green eggs and ham!".
I want the result
if {cat.is} in a hat then I eat green eggs and ham (comma and exclamation symbol removed)
Note: I want to keep brackets, although braces are OK to omit.
Anyone have a solution for this?
You can use
"""[\p{P}\p{S}&&[^.]]+""".toRegex()
The [\p{P}\p{S}&&[^.]]+ pattern matches one or more (+) punctuation proper (\p{P}) or symbol (\p{S}) chars other than dots (&&[^.], using character class subtraction).
See a Kotlin demo:
println("a-b)h.".replace("""[\p{P}\p{S}&&[^.]]+""".toRegex(), ""))
// => abh.
I had an issue with this regex:
(\{(([^\p{Space}][^\p{Punct}])+)\})
The problem is in number of chars. If I typing even number of chars it's works, when odd - not. I was trying to replace '+' with '?' or '*', but result still the same. How can I fix this?
I expect from this regex to block such strings: {%,$ #fd}. And allow this:
{F} or {F242fFSf23}.
Currently, it matches a {, then 1 or more repetitions of 2 chars, a non-space and then a non-punctuation, and then a }, hence you cannot use 1 char in between {...}.
To fix that, you need to use both the character classes inside bracket expression:
\{[^\p{Punct}\p{Space}]+\}
or
\{[^\p{P}\p{S}\s]+\}
Details
\{ - a { char
[^\p{Punct}\p{Space}]+ - 1 or more repetitons (+) of any char that does not belong to the \p{Punct} (punctuation) or \p{Space} (whitespace) class.
\} - a }.
Note that if the contents between the braces can only include ASCII letters or digits (in regex, [A-Za-z0-9]+), you may even use a mere
\{[A-Za-z0-9]+\}
Disassembling your regex... the reason why it only accepts an even number in between is the following part:
([^\p{Space}][^\p{Punct}])+
This basically means: something which isn't a space, exactly 1 character and something which isn't a ~punct, exactly 1 character and this several times... so exactly 1 + exactly another 1 are exactly 2 characters... and this several times will always be even.
So what you probably rather want is the following:
[^\p{Space}\p{Punct}]+
for the part shown above... which will result in the following for your complete regex:
\{[^\p{Space}\p{Punct}]+}
that of course can be simplified even more. I leave that up to you.
I have declared a regex for password validation purposes in strings.xml file.
The criteria is
-should be atleast 8 characters
-should contain atleast one upper case letter
-should contain atleast one lower case letter
-should contain atleast one special character within these "##$%^+&="
So my whole regex looks like this now
^(?=.[0-9])(?=.[a-z])(?=.[A-Z])(?=.[##$%^+&=])(?=\S+$).{8,}$
But when I enter this, I get an error saying that & is
"Unescaped or non terminated character entity/reference"
So instead I used the escape sequence as & but the validation fails for &
I would b glad if anyone could help me out on this!!
Use * quantifers in the look-aheads. Right now, you check if 2nd character in the string meets your conditions. We need to test them all in the string.
^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[##$%^+&=])(?=\S+$).{8,}$
Here is a demo.
EDIT
Since the regex is located inside the XML code, it should be properly encoded. Or, use it inside CDATA block.
Are you missing a parameter in your curly braces? The last bit "{8,}" seems off.
I want to check if a String has a specified structure. I think regex would be the best way to test the String, but I have never used regex before and have sadly no clue how it works. I watched some explanations on stackoverflow, but I couldn't find a good explanation how the regex pattern was created.
My String gets returned from a DataMatrix scanner. For example
String contained = "~ak4,0000D"
Now I want to test this String, if it matches the pattern from the regex.
The String starts everytime with the "~".
After this, two lower cased charactes follow in this example "ak".
After this, there follows a six character long value "4,0000". Main problem here, because the comma can sit anywhere in this value, but the comma must be contained in it. For example it can be ",16000" or "150,00" or "2,8000".
At the last position there must be one of this characters A B C D E F G H J K L M in uppercase contained.
I hope some of you guys can help me.
The regex would be ~[a-z]{2}(?=[\d\,]{6})((\d)*\,(\d)*)[A-H|J-M]{1}$ You can create and test expressions here
boolean isMatch(String STRING_YOU_WANT_TO_MATCH)
{
Pattern patt = Pattern.compile(YOUR_REGEX_PATTERN);
Matcher matcher = patt.matcher(STRING_YOU_WANT_TO_MATCH);
return matcher.matches();
}
You need to use a positive lookahead based regex like below.
System.out.println("~ak4,0000D".matches("~[a-z]{2}(?=\\d*,\\d*.$)[\\d,]{6}[A-HJ-M]"));
System.out.println("~fk,10000D".matches("~[a-z]{2}(?=\\d*,\\d*.$)[\\d,]{6}[A-HJ-M]"));
System.out.println("~jk400,00D".matches("~[a-z]{2}(?=\\d*,\\d*.$)[\\d,]{6}[A-HJ-M]"));
System.out.println("~ak4,0000D".matches("~[a-z]{2}(?=\\d*,\\d*.$)[\\d,]{6}[A-HJ-M]"));
System.out.println("~fk10000,D".matches("~[a-z]{2}(?=\\d*,\\d*.$)[\\d,]{6}[A-HJ-M]"));
System.out.println("~jk400,00I".matches("~[a-z]{2}(?=\\d*,\\d*.$)[\\d,]{6}[A-HJ-M]"));
System.out.println("~ak40000,Z".matches("~[a-z]{2}(?=\\d*,\\d*.$)[\\d,]{6}[A-HJ-M]"));
System.out.println("~fky,10000D".matches("~[a-z]{2}(?=\\d*,\\d*.$)[\\d,]{6}[A-HJ-M]"));
System.out.println("~,jk40000D".matches("~[a-z]{2}(?=\\d*,\\d*.$)[\\d,]{6}[A-HJ-M]"));
Output:
true
true
true
true
true
false
false
false
false
One thing you need to know about regular expressions are that they are a family of things, not one specific thing. There are rather a lot of distinct but similar regular expression languages, and the facilities supporting them vary from programming language to programming language.
Here is a regex pattern that will work in most regex languages to match your strings:
"^~[a-z][a-z]((,[0-9][0-9][0-9][0-9][0-9])|([0-9],[0-9][0-9][0-9][0-9])|([0-9][0-9],[0-9][0-9][0-9])|([0-9][0-9][0-9],[0-9][0-9])|([0-9][0-9][0-9][0-9],[0-9])|([0-9][0-9][0-9][0-9][0-9],))[A-HJ-M]$"
The '^' anchors the pattern to the beginning of the string, and the '$' anchors it to the end, so that the pattern must match the whole string as opposed to a substring. Characters enclosed in square brackets represent "character classes" matching exactly one character from among a set, with the two characters separated by a '-' representing a range of characters. The '|' separates alternatives, and parentheses serve to group subpatterns. For some regex engines, the parentheses and '\' symbols need to be escaped via a preceeding '\' character to have these special meanings instead of representing themselves.
A more featureful regex language can allow that to be greatly simplified; for example:
"^~[a-z]{2}[0-9,]{6}(?<=[a-z][0-9]*,[0-9]*)[A-HJ-M]$"
The quantifiers "{2}" and "{6}" designate that the preceding subpattern must match exactly the specified number of times (instead of once), and the quantifier "*" designates that the preceding subpattern may match any number of times, including zero. Additionally, the "(?<= ...)" is a zero-length look-behind assertion, which tests whether the previous characters of the input match the given sub-pattern (in addition to having already matched the preceding sub-pattern); the characters must also match the subsequent sub-pattern (which does consume them). The '.' metacharacter and '*' quantifier are supported in pretty much all regex languages, but assertions and curly-brace quantifiers are less widely supported. Java's and Perl's regular expression languages will both understand this pattern, however.
~[a-z]{2}[\d|\,]{6}[A-M]
I'm no pro at regex though,but I used this site everytime to build my pattern:
RegExr
Use it like this in your code:
Pattern pattern = Pattern.compile(yourPatternAsAString);
Matcher matcher = pattern.matcher(yourInputToMatch);
if(matcher.matches()) {
// gogogo
}
I am currently working on an app, and I have noticed that if I assign any element (e.g. Textview) a numeric id value (such as android:id="#+id/1") - I get an error and it will not compile until I add a letter to the id.
My questions are:
1) Why are we not able to use numeric values?
2) Are there any other requirements of R id's?
Just trying to better understand the logic behind this..
I have tried searching with not much luck...
Thanks
taken from http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html
Variable names are case-sensitive. A variable's name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign "$", or the underscore character "_"
i believe this goes the same for R.id's. meaning they have to start with a letter, "$" or "-".
for your second question , the convention for R.id is that it should be all lower case and no spaces between words just underscore "_".
To my understanding it is just Java convention. For example, you wouldn't call a variable "1" you would call it "one". It is so the compiler can differentiate between numbers and strings. I recommend just labeling the ids based on what it is going to do, makes it easier on you.
Variables can only start with letters or underscores. Other than that, there are no requirements.