Create regular expression using android pattern - android

I have a sample message . I need to create a regular expression to validate using android pattern.
sample message :
ERR|any digit|any digit;
checking validation:
1.Starting fixed characters :ERR
separator character :|
digit after | character
Message termination ;
I have tried like this way:^{ERR}+{|}+\d+{|}+\d+{;}$
Am I right? Please help to solve my problem.

The corrected regex you gave would be ^(ERR)+(\\|)+\\d+(\\|)+\\d+;$. Brackets are used for grouping, not braces. Also, in regex, + is used to represent "one or more of the previous expression". So writing (ERR)+ means "one or more of the string 'ERR'", so strings like "ERRERR|123|456;" would be matched (same thing goes for the pipe characters) - this is not what you are trying to do, I assume.
Having said that, try this: "^ERR\\|\\d+\\|\\d+;$"

Related

Kotlin Android allow only emojis and letters in a text

I've been trying to find a good way to be able to keep only emojis and letters in a given text, but every article I found, I didn't have success with .
I've tried to use regex, but seems that I can not make it work.
I've tried to use emoji4j but it seems that this library is working with emojis in this form ":)", which don't help me, because my emojis are groups of unicode characters.
The result I want is the following :
"This is. a text ๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ,,1234" => "This is a text ๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ"
"๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ" => "๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ"
"๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ๐Ÿ˜ƒ123abc๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ" => "๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ๐Ÿ˜ƒabc๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ"
Here's the emoji regex : ?:[\u2700-\u27bf]|(?:[\ud83c\udde6-\ud83c\uddff]){2}|[\ud800\udc00-\uDBFF\uDFFF]|[\u2600-\u26FF])[\ufe0e\ufe0f]?(?:[\u0300-\u036f\ufe20-\ufe23\u20d0-\u20f0]|[\ud83c\udffb-\ud83c\udfff])?(?:\u200d(?:[^\ud800-\udfff]|(?:[\ud83c\udde6-\ud83c\uddff]){2}|[\ud800\udc00-\uDBFF\uDFFF]|[\u2600-\u26FF])[\ufe0e\ufe0f]?(?:[\u0300-\u036f\ufe20-\ufe23\u20d0-\u20f0]|[\ud83c\udffb-\ud83c\udfff])?)*|[\u0023-\u0039]\ufe0f?\u20e3|\u3299|\u3297|\u303d|\u3030|\u24c2|[\ud83c\udd70-\ud83c\udd71]|[\ud83c\udd7e-\ud83c\udd7f]|\ud83c\udd8e|[\ud83c\udd91-\ud83c\udd9a]|[\ud83c\udde6-\ud83c\uddff]|[\ud83c\ude01-\ud83c\ude02]|\ud83c\ude1a|\ud83c\ude2f|[\ud83c\ude32-\ud83c\ude3a]|[\ud83c\ude50-\ud83c\ude51]|\u203c|\u2049|[\u25aa-\u25ab]|\u25b6|\u25c0|[\u25fb-\u25fe]|\u00a9|\u00ae|\u2122|\u2139|\ud83c\udc04|[\u2600-\u26FF]|\u2b05|\u2b06|\u2b07|\u2b1b|\u2b1c|\u2b50|\u2b55|\u231a|\u231b|\u2328|\u23cf|[\u23e9-\u23f3]|[\u23f8-\u23fa]|\ud83c\udccf|\u2934|\u2935|[\u2190-\u21ff] .
If I try something like :
val regex = "the_whole_regex_above | [^a-zA-Z]".toRegex()
myText.replace(regex,""), it won't replace anything, basically every character will pass
Basically I want to achieve pretty much the same thing as in this question, but using Kotlin.
You want to remove all punctuation, symbols (other than those used to form emojis) and digits.
To do that, you may use
myText = myText.replace("""[\p{N}\p{P}\p{S}&&[^\p{So}]]+""".toRegex(), "")
See the online Kotlin demo.
Details
[ - start of a character class that matches:
\p{N} - any Unicode digit
\p{P} - any Unicode punctuation proper
\p{S} - any Unicode symbol
&&[^\p{So}] - BUT the Unicode symbols belonging to Symbol, other Unicode category that are mostly used to form emojis
]+ - 1 or more occurrences.

Combination of rules with Regex

In an android project, im trying to validate a password that the user inputs, and it must follow some rules
The rules are:
it must have 7 characters and 3 of the following conditions
**
-One lowercase character
-One uppercase character
-One number
-One special character
**
for example:
asd123!!!
PPPppp000
TTT999###
i was trying with this regex
^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!#$%^&*-]).{7,}+$
but this enforces all rules at same time.
The approach is wrong here. The regex you created looks like a monster from under the bed, and is highly illegible even for someone regex-literate.
Why not split it into 4 (or as much as there are rules) regexes and check against whether 3 of them return a match? Not only will you make your regexes cleaner, but you will be able to add more rules if need be without changing whole regex.
You can also use inbuilt methods for checking (if applicable under Android development kit).
Some pseudocode would look like this:
result1 = Regex.IsMatch(password, rule1regex)
result2 = Regex.IsMatch(password, rule2regex)
...
resultN = Regex.IsMatch(password, rule3regex)
if(three_out_of_four_rules_apply)
password_valid = true
You can also apply method suggested in comments by #pskink and iterate over each character of a password and set the output accordingly.
Without going into the details of your lookaheads (which seem correct), here's how you would need to implement "three out of four criteria" in pure regex :
(?=.*A)(?=.*B)(?=.*C)|(?=.*A)(?=.*B)(?=.*D)|(?=.*A)(?=.*C)(?=.*D)|(?=.*B)(?=.*C)(?=.*D)
You can test it here.
Factorizing doesn't really make it better :
(?=.*A)(?:(?=.*B)(?=.*(?:C|D))|(?=.*C)(?=.*D))|(?=.*B)(?=.*C)(?=.*D)
I obviously recommend using a higher level language to implement these sorts of constraints.

How to return apostrophe when using Google Translate API for Android?

I have an Android application that uses Google Translate API.
Everything works great, including when I tried to translate phrases that include apostrophe like "We've eaten" to Spanish.
However, problems occur when the translation result I should be getting back contains an apostrophe. For example, when I translate a Spanish phrase, "A ver", into English, it returns "Let&#39s see" with a ";" after "9". It seems like whenever I have a phrase that should return an apostrophe, it returns "&#39" with a ";" after "9". (Not placing ";" after "9" because it gets converted to an apostrophe by stackoverflow).
I can think of a way to solve it. After I get the translation result, I can match the string for ""&#39" + ";" and replace it with an apostrophe.
However, I don't feel like this is the way I should approach it. It's very unlikely that a user will actually type in "&#39" as an input for translation, but hard coding a manual conversion like this seems like it might cause problems down the road. I'll love to hear your thoughts on this.
Please let me know how I should fix/approach this issue.
Thank you!
The best solution is to add &format=text to your query.
You are correct hard codding is not solution,
But you can convert this HTML entity back to apostrophe, by Using HTML classes provided already.
Html.fromHtml((String) "Let's see").toString()
Above code will convert any valid HTML entity.
I Hope this is what you are looking for.
Thanks Guillaume. For those using php.
$translation = $translate->translate($stringToTranslate, ['target' => $target, 'format' => 'text']);
Thanks Guillaume. For those using go. (api v3)
req := &translatepb.TranslateTextRequest{
MimeType: "text/plain", // add this line to request
}

Password regex string in strings.xml file in android

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.

How to use solo.searchText for searching some text consisting of special characters

I am using robotium to test an android project.I have a testcase where i need to test a message consisting of special characters is posted correctly.
So I created a constant consisting of special characters :
public static final String PostMessageWithSpecialchars = "Hey hi,* Have a good day*.:()[]-=/&!?"'+;##";
and i am using following code to search it and assert that the posted message is exactly like the message in the constant PostMessageWithSpecialchars
assertTrue(solo.searchText(PostMessageWithSpecialchars));
but the test fails at assertTrue line.
What to do to search the PostMessageWithSpecialchars text?I dont want to use escape characters because that will ignore special characters.I want to make sure that the special characters in the PostMessageWithSpecialchars message are posted correctly.
The method solo.searchText() accept regex pattern. In your search string you are using special characters that is used for patters. You can quote them to find any text:
assertTrue(solo.searchText(Pattern.quote(PostMessageWithSpecialchars)));

Categories

Resources