In debug mode I can see that locator of one of the element on the page is: By.name: NameOfMyElement_123.
The question is, how can I parse the following string (By.name: NameOfMyElement_123) in Java in order to have the type of my locator (name) and value (NameOfMyElement_123) ?
String[] split = "By.name: NameOfMyElement_123".split(" ");
or
Pattern p = Pattern.compile("([\\w.]*): ([\\w]*_[\\d]*)");
Matcher m = p.matcher("By.name: NameOfMyElement_123");
while (m.find()){
System.out.println(m.group(1));
System.out.println(m.group(2));
}
You could use split(). In this case, it's best to split with :
String[] splittedText = element.split(':');
String type = splittedText[0].trim();
String value = splittedText[1].trim();
Nothing fancy is necessary, two split() methods are enough:
String[] firstSplit = element.split(':');
String[] secondSplit = firstSplit[0].split('.');
String type = secondSplit[1].trim(); // will result in "name"
String value = firstSplit[1].trim(); // will result in "NameOfMyElement_123"
Related
I want to remove [ ] braces from the below string-
"[maths=100, english=20]"
I have tried doing it in following ways but in both the trials it is not removing the end ] brace.
Approach 1:
String[] metrics= "[maths=100, english=20]";
String[] value = metrics[1].split("\\[");
String[] finalValue = value[1].split("\\]");
System.out.println(finalValue[0]); // this should give string as maths=100, english=20
Approach 2:
String[] metrics= "[maths=100, english=20]";
String[] value = metrics[1].split("\\[\\]");
System.out.println(finalValue[1]); // this should give string as maths=100, english=20
Can anyone guide me where i am doing it wrong?
Try this code
String metrics= "[maths=100, english=20]";
String[] split = metrics.split("\\[|]");
System.out.println(split[1]);
it prints
"maths=100, english=20"
Or you can simply replace all [ and ] character
String metrics = "[maths=100, english=20]";
metrics = metrics.replace("[", "").replace("]", "");
System.out.println(metrics);
If you simply want to trim and clean your data then you can do a simple check and substring.
String input = ...;
String cleanedInput = input.trim();
if (cleanedInput.startsWith("[") && cleanedInput.endsWith("]")) {
cleanedInput = cleanedInput.substring(1, cleanedInput.length() - 1);
System.out.println(cleanedInput);
}
If you're wanting to match and capture from a larger set of data then you can use RegEx patterns with capture groups to match and capture the data you want.
For parsing a proper document structure though you should try to use a real parser but if you truly are just trying to match and capture some simple data then RegEx will often be ok.
String input = ...;
// RegEx pattern "\[([^\[\]]*)\]" anything inside braces except other braces
Pattern pattern = Pattern.compile("\\[([^\\[\\]]*)\\]");
Matcher matcher = pattern .matcher(input);
while (matcher.find()) {
String data = matcher.group(1);
System.out.println(data);
}
You can simply replace the braces like this:
String s = "[maths=100, english=20]";
s = s.replace("[", "").replace("]", "");
System.out.println(s);
I have the follow urls.
https://docs.google.com/spreadsheets/d/1mrsetjgfZI2BIypz7SGHMOfHGv6kTKTzY0xOM5c6TXY/edit#gid=1842172258
https://docs.google.com/a/example.com/spreadsheets/d/1mrsetjgfZI2BIypz7SGHMOfHGv6PTKTzY0xOM5c6TXY/edit#gid=1842172258
https://docs.google.com/spreadsheets/d/1mrsetjgfZI2BIypz7SGHMOfHGv6kTKTzY0xOM5c6TXY
Foreach url, I need to extract the sheet id: 1mrsetjgfZI2BIypz7SGHMOfHGv6PTKTzY0xOM5c6TXY into a java String.
I am thinking of using split but it can't work with all test cases:
String string = "https://docs.google.com/spreadsheets/d/1mrsetjgfZI2BIypz7SGHMOfHGv6kTKTzY0xOM5c6TXY/edit#gid=1842172258";
String[] parts = string.split("/");
String res = parts[parts.length-2];
Log.d("hello res",res );
How can I that be possible?
You can use regex \/d\/(.*?)(\/|$) (regex demo) to solve your problem, if you look closer you can see that the ID exist between d/ and / or end of line for that you can get every thing between this, check this code demo :
String[] urls = new String[]{
"https://docs.google.com/spreadsheets/d/1mrsetjgfZI2BIypz7SGHMOfHGv6kTKTzY0xOM5c6TXY/edit#gid=1842172258",
"https://docs.google.com/a/example.com/spreadsheets/d/1mrsetjgfZI2BIypz7SGHMOfHGv6PTKTzY0xOM5c6TXY/edit#gid=1842172258",
"https://docs.google.com/spreadsheets/d/1mrsetjgfZI2BIypz7SGHMOfHGv6kTKTzY0xOM5c6TXY"
};
String regex = "\\/d\\/(.*?)(\\/|$)";
Pattern pattern = Pattern.compile(regex);
for (String url : urls) {
Matcher matcher = pattern.matcher(url);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
}
Outputs
1mrsetjgfZI2BIypz7SGHMOfHGv6kTKTzY0xOM5c6TXY
1mrsetjgfZI2BIypz7SGHMOfHGv6PTKTzY0xOM5c6TXY
1mrsetjgfZI2BIypz7SGHMOfHGv6kTKTzY0xOM5c6TXY
it looks like the id you are looking for always follow "/spreadsheets/d/" if it is the case you can update your code to that
String string = "https://docs.google.com/spreadsheets/d/1mrsetjgfZI2BIypz7SGHMOfHGv6kTKTzY0xOM5c6TXY/edit#gid=1842172258";
String[] parts = string.split("spreadsheets/d/");
String result;
if(parts[1].contains("/")){
String[] parts2 = parts[1].split("/");
result = parts2[0];
}
else{
result=parts[1];
}
System.out.println("hello "+ result);
Using regex
Pattern pattern = Pattern.compile("(?<=\\/d\\/)[^\\/]*");
Matcher matcher = pattern.matcher(url);
System.out.println(matcher.group(1));
Using Java
String result = url.substring(url.indexOf("/d/") + 3);
int slash = result.indexOf("/");
result = slash == -1 ? result
: result.substring(0, slash);
System.out.println(result);
Google use fixed lenght characters for its IDs, in your case they are 44 characters and these are the characters google use: alphanumeric, -, and _ so you can use this regex:
regex = "([\w-]){44}"
match = re.search(regex,url)
I've to replace a set of substrings in a String with another substrings for example
"^t" with "\t"
"^=" with "\u2014"
"^+" with "\u2013"
"^s" with "\u00A0"
"^?" with "."
"^#" with "\\d"
"^$" with "[a-zA-Z]"
So, I've tried with:
String oppip = "pippo^t^# p^+alt^shefhjkhfjkdgfkagfafdjgbcnbch^";
Map<String,String> tokens = new HashMap<String,String>();
tokens.put("^t", "\t");
tokens.put("^=", "\u2014");
tokens.put("^+", "\u2013");
tokens.put("^s", "\u00A0");
tokens.put("^?", ".");
tokens.put("^#", "\\d");
tokens.put("^$", "[a-zA-Z]");
String regexp = "^t|^=|^+|^s|^?|^#|^$";
StringBuffer sb = new StringBuffer();
Pattern p = Pattern.compile(regexp);
Matcher m = p.matcher(oppip);
while (m.find())
m.appendReplacement(sb, tokens.get(m.group()));
m.appendTail(sb);
System.out.println(sb.toString());
But it doesn't work. tokens.get(m.group()) throws an exception.
Any idea why?
You don't have to use a HashMap. Consider using simple arrays, and a loop:
String oppip = "pippo^t^# p^+alt^shefhjkhfjkdgfkagfafdjgbcnbch^";
String[] searchFor =
{"^t", "^=", "^+", "^s", "^?", "^#", "^$"},
replacement =
{"\\t", "\\u2014", "\\u2013", "\\u00A0", ".", "\\d", "[a-zA-Z]"};
for (int i = 0; i < searchFor.length; i++)
oppip = oppip.replace(searchFor[i], replacement[i]);
// Print the result.
System.out.println(oppip);
Here is an online code demo.
For the completeness, you can use a two-dimensional array for a similar approach:
String oppip = "pippo^t^# p^+alt^shefhjkhfjkdgfkagfafdjgbcnbch^";
String[][] tasks =
{
{"^t", "\\t"},
{"^=", "\\u2014"},
{"^+", "\\u2013"},
{"^s", "\\u00A0"},
{"^?", "."},
{"^#", "\\d"},
{"^$", "[a-zA-Z]"}
};
for (String[] replacement : tasks)
oppip = oppip.replace(replacement[0], replacement[1]);
// Print the result.
System.out.println(oppip);
In regex the ^ means "begin-of-text" (or "not" within a character class as negation). You have to place a backslash before it, which becomes two backslashes in a java String.
String regexp = "\\^[t=+s?#$]";
I have reduced it a bit further.
What kind of method would I use to make this:
http://www.site.net/files/file1.zip
To
file1.zip?
String yourString = "http://www.site.net/files/file1.zip";
int index = yourString.lastIndexOf('/');
String targetString = yourString.substring(index + 1);
System.out.println(targetString);// file1.zip
String str = "http://www.site.net/files/file1.zip";
str = str.substring(str.lastIndexOf("/")+1);
You could use regex to extract the last part:
#Test
public void extractFileNameFromUrl() {
final Matcher matcher = Pattern.compile("[\\w+.]*$").matcher("http://www.site.net/files/file1.zip");
Assert.assertEquals("file1.zip", matcher.find() ? matcher.group(0) : null);
}
It'll return only "file1.zip". Included here as a test as I used it to validate the code.
Use split:
String[] arr = "http://www.site.net/files/file1.zip".split("/");
Then:
String lastPart = arr[arr.length-1];
Update: Another simpler way to get this:
File file = new File("http://www.site.net/files/file1.zip");
System.out.printf("Path: [%s]%n", file.getName()); // file1.zip
I am trying parse out 3 pieces of information from a String.
Here is my code:
text = "H:7 E:7 P:10";
String pattern = "[HEP]:";
Pattern p = Pattern.compile(pattern);
String[] attr = p.split(text);
I would like it to return:
String[0] = "7"
String[1] = "7"
String[2] = "10"
But all I am getting is:
String[0] = ""
String[1] = "7 "
String[2] = "7 "
String[3] = "10"
Any suggestions?
A not-so-elegant solution I just devised:
String text = "H:7 E:7 P:10";
String pattern = "[HEP]:";
text = text.replaceAll(pattern, "");
String[] attr = text.split(" ");
From the javadoc, http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html#split(java.lang.CharSequence) :
The array returned by this method contains each substring of the input
sequence that is terminated by another subsequence that matches this
pattern or is terminated by the end of the input sequence.
You get the empty string first because you have a match at the beginning of the string, it seems.
If I try your code with String text = "A H:7 E:7 P:10" I get indeed:
A 7 7 10
Hope it helps.
I would write a full regular expression like the following:
Pattern pattern = Pattern.compile("H:(\\d+)\\sE:(\\d+)\\sP:(\\d+)");
Matcher matcher = pattern.matcher("H:7 E:7 P:10");
if (!matcher.matches()) {
// What to do!!??
}
String hValue = matcher.group(1);
String eValue = matcher.group(2);
String pValue = matcher.group(3);
Basing on your comment I take it that you only want to get the numbers from that string (in a particular order?).
So I would recommend something like this:
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher("H:7 E:7 P:10");
while(m.find()) {
System.out.println(m.group());
}