In my android app i have to make a control on a edit text that contains an address,
for now my control is:
Indirizzo.toString().matches("[\\sa-zA-Z0-9]*")
But I must also add other characters to control,
such as: \ . n ° ,
How can I change the regular expression?
You might consider using something like so: [\sa-zA-Z0-9\\\.n°/]*.
That being said, that particular regular expression will also match an empty field. If you do not want this, simply replace the * (matches 0 or more repetitions of) with + (matches one or more repetitions of).
The \ is a special character, which needs to be escaped, hence the extra \ infront of it. The . is also a special character which also needs to be escaped.
So, in Java, you would need to use something like so: [\\sa-zA-Z0-9\\\\\.n°/]*
You can make the regex accept all ASCII characters by using the following character class:
[\x00-\x7F]
Or, if you'd like to match those specific characters only, that is \ . n ° ,, simply add them to the character class:
[\\sa-zA-Z0-9\.n°,]*
Also, you should use + rather than * to eliminate zero-char matches.
Related
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+;$"
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 newt o regular expressions and been using tutorials, but the regular express I have works sometimes, but doesn't all the time. I am getting my numbers out of the contact list from my android phone. I am trying to get rid of all spaces, '(', ')', and '-'
For example:
1. (555) 867-5309 -> 5558675309
2. 1555-555-5555 -> 15555555555
3. 555-555-5555 -> 5555555555
This is the line I am using
String formatphone = contactPhone.replaceAll("\\s()-","");
For some numbers it only returns number and sometimes it doesn't change the format.
Is it correct? Do i need to format something because I am taking it out of the phone's contact list?
Put the desired characters in a character class:
String formatphone = contactPhone.replaceAll("[ ()-]","");
Ensure that you put the hyphen - at either end.
Try using this:
String formatphone = contactPhone.replaceAll("^.*[\\s\\(\\)-].*", "");
As a regular expression you're defining a set using []. In that set you include any character you want to be replaced. As ( and ) are special meaning characters, you have to escape them. As the - is a special character used to design ranges, it has to be the last character of your set, so if nothing is behind it, it's not a range, but just that character (you could escape it too, though).
On Android Jellybean 4.1 is it possible to escape a LIKE wildcard character and still have the like use the index?
Use:
where field like 'xyz\_abc'
as opposed to:
where field like 'xyz_abc'
Does escaping wildcards work on Android? And will it still use the index if the wildcard is escaped?
What I am currently doing is:
where field like 'xyz_abc' and lower(field) = lower('xyz_abc')
Which is horribly inefficient due to the wildcard character.
Thanks
You need to use the ESCAPE clause:
where field like 'xyz\_abc' escape '\'
See the section The LIKE and GLOB operators in the SQLite Documentation.
hi I'm just creating a header for a app that uses twitter and just wanting to know how to show the # as text i put it in a string and it doesn't like it
<string name="twit">#evosdfresh</string>
then i call this string it doesn't work if i remove the # it works so its the # thats throwing it off
Try replacing the # char with its utf-8 xml decimal representation: #
EDIT: given the xml entity does not work, use the unicode definition: \u0040
If you have the # as the first character of the string, then you need to escape it with a backslash, e.g.
<string name="twit">\#evosdfresh</string>
If it's in the middle of the string you don't need the backslash.
Try # instead of #. If that doesn't work, you could also try escaping it, with 2 #'s instead of 1, or a backslash before it. ##evosdfresh or \#evosdfresh
My idea is to replace it with an #. Tell me if it works, I'd be interested to know, too ;)