Reqular expressions in Android with input that contains []{}" - android

I am trying to extract the X and Y component using a regular expression from the following data:
{"SearchResults":[{"PageCount":"0"},
{"SEARCHVAL":"530106","CATEGORY":"Building",
"X":"103.8907","Y":"1.3537"}]}
This is the pattern I tried to no avail:
Pattern p1 = Pattern.compile("\\\"X:\\\"([0-9.]*)\\\",\\\"Y\\\":\"([0-9.]*)\\\"");
Matcher m1 = p1.match(result);
if( m1.matches() ){
print("match found");
}
I have also tried the following without any luck:
Pattern.compile("\"X:\"([0-9.]*)\",\"Y\":\"([0-9.]*)\"");
This should be easy, but yet I have been stuck here for the past 2 hours.

If you want regex to parse it, then you can use:
"X":"(\d+(?:\.\d+)?)","Y":"(\d+(?:\.\d+)?)"
Regex Demo
You are missing a quote(") after X in your regex. Though I recommend not to use your regex because it will also match 1.1.2.3
In JAVA:
\"X\":\"(\\d+(?:\\.\\d+)?)\",\"Y\":\"(\\d+(?:\\.\\d+)?)\"

This RegEx will work:
"X":"([0-9.]*)","Y":"([0-9.]*)"
The 1st Capture Group contains the X value, and the 2nd Capture Group contains the Y value
Live Demo on Regex101
Which means your Pattern.compile should be:
Pattern.compile("\"X\":\"([0-9.]*)\",\"Y\":\"([0-9.]*)\"");
Note that you may need to add .* at the start of the RegEx for it to work.

for others that are stuck with the same problem, the reason why .* is needed at the front/end is because i used the wrong method call.
according to http://developer.android.com/reference/java/util/regex/Matcher.html#matches()
public boolean matches ()
Added in API level 1 Tries to match the Pattern against the entire
region (or the entire input, if no region has been set).
It needs to match the entire region.
to do any match, i would need to use find() instead.
public boolean find ()
Added in API level 1 Moves to the next occurrence of the pattern in
the input. If a previous match was successful, the method continues
the search from the first character following that match in the input.
Otherwise it searches either from the region start (if one has been
set), or from position 0.
hope this is useful to others.
String.matches() provides more insights to matches() method http://developer.android.com/reference/java/lang/String.html
public boolean matches (String regularExpression)
This method returns true only if the regular
expression matches the entire input string. A common mistake is to
assume that this method behaves like contains(CharSequence); if you
want to match anywhere within the input string, you need to add .* to
the beginning and end of your regular expression. See matches(String,
CharSequence).

Related

Array in Between Condition

How do I implement this condition as shown in this image.
That's just in 2..4, i.e. in the integer range from 2 to 4, including the 4.
The ≤ is just some hint text provided by the IDE, similar to how you can see argument names in function calls - it's not part of the code, it's not really there. They added it to make it clearer what the .. operator does (vs until)
This might be because they're introducing a new operator, ..< which seems intended to replace until, or maybe they've been there a while! You can turn them on and off here in the settings:

Android: Is there an object in Kotlin that contains all characters like colon, semicolon etc

Right now I am starting a new project and I usually had a constant class for symbols like:
object Symbols{
const val CHAR_COLON = ":"
const val CHAR_COMMA = ","
const val CHAR_SEMICOLON = ";"
etc.
}
Would be great if a class like this was already added in Java or Kotlin but I don't seem to find anything like this. Does anyone know of such a thing? Thanks.
I'm not sure why you want to define constants for individual characters (just use the char, it's a constant!) but the Unicode standard defines a bunch of categories for characters, and you can access those through the Java Character class, which also has some methods for getting a character's type.
Kotlin gives you a CharCategory enum class which contains all these categories, and some functions that make it a little easier - for example you can do this:
println(CharCategory.OTHER_PUNCTUATION.contains(','))
>> true
But for example, '-' is not in the OTHER_PUNCTUATION category, aka category Po. That comes under DASH_PUNCTUATION, Pd. If you notice, all the punctuation category codes start with P, so you could do this kind of thing:
val punctuationCategories
= CharCategory.values().filter { it.code.first() == 'P' }
val Char.isPunctuation: Boolean get() = punctuationCategories.any { it.contains(this) }
println('-'.isPunctuation)
>> true
That's just a basic overview and pointing out this stuff is baked into the Unicode standard - I don't really know a lot about it (I can't see a more convenient way to do this but I could be wrong) and I'm not sure if it's helpful, but there it is!
Kotlin does have an object full of constants for "Unicode symbols used in proper Typography" for some reason though! Also a couple with typos in the name so they had to deprecate them, now that's what I call typography
edit: I should point out that Unicode aims to represent every writing system that ever existed, so its scope of what counts as "punctuation" might be a bit broader than what you're looking for! It depends if you're just trying to check a character, or if you want to create a collection of punctuation characters. If you just want to limit things to a particular Charset, like US_ASCII or whatever, I'm not sure how you can get access to all the characters that covers

How to use AndroidViewClient findViewWithText() with regex

I have a script to monitor the Notifications screen page.
I can open it via "Culebra" option "UiDevice" -> "Open Notifications".
The notifications error message from some apps have starting characters which are changing, only a constant pattern like "error for" is common but located at different position of the TextView error messages.
Therefore I can't use a regex with the method findViewWithText(regex) as it seems to use a regex match() instead of regex search(). Another solution for my problem is to use traverse() method with my own transform method which can do a regex search() of the view attribute text, but I can't figure out how to pass a parameter like a regex to my own transform method!?
This works for me to touch on a notification with text USB debugging connected:
vc.findViewWithTextOrRaise(re.compile('.*USB.*'), root=vc.findViewByIdOrRaise('id/no_id/3')).touch()
vc.sleep(_s)
notice this is a modified culebra script, that's why findViewWithTextOrRaise() is using the root argument to limit the search to the subtree which may not be needed in all cases, but it's safer to use.
It's worth to mention, that this works too
vc.findViewWithTextOrRaise(re.compile('.*debugging.*'), root=vc.findViewByIdOrRaise('id/no_id/3')).touch()

WebView number of matches

I am using "findAll" on a webview, it works correctly, but do not know how I can retrieve the number of matches for if greater than 0 to show a button in that case.
Refer this
Returns
: the number of occurances of the String "find" that were found
So if you use
int count = mWebView.findAll(searchString);
then count will hold occurrence of a searchString text in webview.
Hope this helps.
Since findAll has this nasty bug returning 0 even if it matches and the function is deprecated:
findAllAsync Seems to be the way to go.
Its a little more work though since you need to implement a WebView.FindListener
The results returned through that are also structured a bit differently and more difficult to handle.
webView.setFindListener(yourFindListener);
webView.findAllAsync("some string")
What also can be tricky is delaying the search until the site is fully rendered which is another tricky topic on its own. (depending on the page and its JS usage)

How do I get containsignorecase for java?

I am trying to use contains for strings coinciding with the search symbols. However, seems like contains does not have an ignore case unlike equals. Is there any way to go around ?
The following line of code is where I am using the same (I have heard of pattern, but how do I use the same in my case? )
searchSymbol.contains(mSearchView.getText().toString()
Thanks!
Justin
You can just convert them both to lower case and use contains() as normal:
searchSymbol.toLowerCase().contains(mSearchView.getText().toString().toLowerCase())
You could write your own function to do this if you want to keep it clean:
public static boolean containsIgnoreCase(String haystack, String needle){
return haystack.toLowerCase().contains(needle.toLowerCase());
}
You could use the function from StringUtils:
org.apache.commons.lang3.StringUtils.containsIgnoreCase("testString", "stSt");
In any case, you're going to want to watch out for unicode characters. They don't play by the normal rules of "case" in most circumstances.

Categories

Resources