I migrated my project from Eclipse to Android Studio and everything worked fine. In this project I have a Regex which should find all image urls in a json-object I get from an API.
This is the Regex I have:
Pattern pattern = Pattern.compile("https?:\\/\\/[^\"]*?\\.(png|jpe?g|img)");
Matcher matcher = pattern.matcher(obj.toString());
while(matcher.find()) {
ImageBackgroundFetcher.getInstance(mActivity).addImageToLoadingQueue(matcher.group());
}
obj is the JSONObject I have with the image urls. It looks like the following and I would like to extract the url of the image (the bold marked part)
{"image":"http:\/\/www.test.de\/media\/2015\/01\/16\/bildtitel.jpg"}
After I migrate the project from Eclipse to Android Studio this Regex isn't working any longer. The matcher.find()-Method did not return true and Android Studio gives me a warning in the code at the regex part "\\/\\/" where it says "redundant character escape"
I already googled but didn't find a solution. Any ideas how to solve the problem would be great, thanks in advance ;)
You can use AS function to check Regex.
Press Shortcut: Alt+Enter → check regexp
Okay, I am able to solve this issue. The correct regex is:
https?:\\\\/\\\\/[^\"]*?\\.(png|jpe?g|img)
I usually use this site to check:
http://regexr.com/
using the pattern:
(https?:\\/\\/.+(png|jpe?g|img))(?:")
and capturing the first group it should work.
Just check in java, escaping \ and / is not the same as the site i linked
Using your expression, the pattern:
https?:\/\/[^\\"]*?\\.(png|jpe?g|img)
should work too. This last string should not be escaped again. Check also your string in the original eclipse project!
It has to do with the way Java treats Regex, it needs String literals. So \\ for a single \
Related
I am facing a strange problem.
I have a solution that contains an Android project and a class library project.
If in that library project I execute this line:
String s = "Código inválido";
This is actually shown when watching the variable: Código inválido.
However, when I do that assignment inside the Activity code, the text is shown correctly as Código inválido.
What may be wrong with this?
The origin of the problem was when I tried to show, in the Activity layout, a text that was assigned in a class library method. After a lot of digging, I found what I expose in this question.
Just in case, I tell you that all files encodings are UTF-8, and in the project settings all encodings are also UTF-8.
This is Android Studio 4.0.1
Thanks
Jaime
I am looking for an efficient method to find and replace in android studio. where I can find a string using regex and replace the selected string with its substring.
e.g.
constants.LOG_FILE_PATH -> SETTINGS['LOG_FILE_PATH']
constants.LOG_DIR -> SETTINGS['LOG_DIR']
I have following regex for find
constants.[A-Z_]*
which selects constants.LOG_FILE_PATH and constants.LOG_DIR
but how to modify the replace string so that the above strings can be replaced by SETTINGS['LOG_FILE_PATH'] and SETTINGS['LOG_DIR'] respectively. I have to find and replace many strings in my project so manual find and replace is little overhead.
Edit 1: If you know how to do in pycharm or intellij-idea it will work in android studio also, thats why i have added tags pycharm and intellij-idea
Perhaps look at Structural Replace. Use the Find Actions shortcut (CMD + Shift + A) and type structural and the option should appear.
I guess something like the following may work:
Search template
constants.$log$
Replacement template
SETTINGS["$log$"]
Disclaimer, this is untested.
I'm using Android Studio 2.2.1 .
I'm trying to create an Android Studio TODO pattern (Settings → Editor → TODO → Patterns) with regular expression that will match the word bug followed by : no matter where this combination written in the sentence.
The following sentences are ones that I want to match:
bug: not working.
BUG: whatever...
this is a BUG: !
In contrast to the above, the following sentences are ones that I don't want to match:
this is a bad bug.
is it that buggy
lots of bugs and no solution.
:BUG doesn't work.
I currently have \bbug\b.* regular expression that works but he also works when : character is missing.
I've tried the following regular expressions without a success:
\bbug:\b.*
\bbug\\:\b.*
I would like to know how can I crate a regular expression pattern that consist of the word bug followed by : as a special character.
You just have to put the colon on the outside of the pattern, like so: \bbug\b:.*
This grabs bug:, BUG:, but not bug, BUG, or :BUG.
I learn android with sample apps from textbook. In the given sample source R.string.something is recognized:
However, in my own EXACT source code, same R.strings are not recognized- highlighted as errors:
Does anyone know how to fix it?
I've had this many time with eclipse, and it was not a setup / code problem on my side. Generally, cleaning, rebuilding and sometime even stopping and restarting eclipse solved it. And yes, it's a pain in the neck ...
Try a clean of your project, if that does nothing then click on the problems tab and see if there's any build related issues, you may be missing a required jar or something!
These are following reason possible.
1-: You import android.R;
2-: Any error in xml files.
3-: Please check you String.xml file may be any error or declare a string more then two time.
I assume you are new to Android.
If so, there are two ways of using text strings in Buttons, textviews and so on:
1) Hardcoded string - you put the text you want in quotation marks (""), for example:
yourTextview.setText("Hardcoded string");
2) You can call the text from your String resources (res/values/strings.xml).
That is a much better approach seeing is is easier to translate, make changes and so on.
In your strings.xml file you can create all your string values, and call them from there.
Like in you example, if in your strings.xml file you have for example:
<string name="delete">This is String resource</string>
you can then call the string from there, like so :
yourTextview.setText(R.string.delete);
Hope this helps!
I am using MonkeyTalk to automate some user test cases for my Android app. Everything is working fine except for when I try and detect a button containing this string:
"Connect\n(Code Required)"
I get this error:
FAILURE: Unable to find Button(Connect\n(Code required))
If I change the button to "Connect" and perform a tap on that value MonkeyTalk has no trouble, but something about the line break must be throwing it off.
After some searching I found this thread that confirmed my suspicious about the line break. There was one suggested fix here, to set the default encoding to UTF-8 (Select the Project > File > Properties > Resources)
However this did not work for me.
I have also tried to find the button using a wildcard like so:
"*(Code Required)"
But this does not seem to be supported either.
Maybe there is an alternative line break character I could use?
Thanks in advance for the help!
Maybe there's a carriage return in there? I know in most text editors a new line actually consists of (carriage return)+(newline).
Also take a look at this:
TextView carriage return not working
Also, depending on how flexible your requirements are, you could use the #N MonkeyId replacement to get the Nth button.
IN javascript you can use below command
app.button("buttonname").tap(x, y);
Use android:contentDesxription="your_component_id" in your view xml file definition or view.setContentDescription("your_component_id"); directly on view in code to make it easy to access in MonkeyTalk.