I was wondering if someone could show me how to extract part of string from a var in a Imacros java script.
WebPageNumber = "Code: ";
WebPageNumber += "TAB T=1" + NewLine;
WebPageNumber += "FRAME NAME="+"ext-comp-1006" + NewLine;
WebPageNumber += "TAG POS=1 TYPE=SPAN ATTR=ID:00B70000007Wo0i_paginator_rpp_target EXTRACT=TXT" + NewLine;
WebPageNumber += "SET !CLIPBOARD {{!EXTRACT}}";
PlayMacro = iimPlay(WebPageNumber);
alert (iimGetLastExtract ());
My Result is
1-75 of 75 Display 10 records per page Display 25 records per page Display 50 records per page Display 100 records per page Display 200 records per page
All I want to take from it is 1 & 75
Since you're already using the JS implementation of iMacros, wouldn't it be easier just to manipulate that information with JS?
var extractString = iimGetLastExtract();
var firstNumber = extractString.substring(0,1);
var secondNumber = extractString.substring(2,2);
That's assuming that you'll always be looking for the first number and the second pair of numbers. You can also use regular expressions with the JavaScript String match() method, but
that'll dump an array of values.
Related
This is a portion of a mab libs game that is reading in a template from one file and then using PrintStream to send out the game results to another wherever a tag in the template such as or exists, the user is prompted to give a value to replace it. The issue that I'm having right now is that the tags with a "-" are not being replaced. One of the requirements is that all "-" be converted to spaces instead so I have some code to do so. As a result of the code that checks for a "-", the template output where tags contain "-" don't get changed to the user input. If I remove the code that checks for -, It works fine but the user prompt cant include a -.
while (each.hasNext()) {
String word = each.next();
if (word.contains("<")) {
int start = word.indexOf("<");
int end = word.indexOf(">");
// word between <>
String x = word.substring(start + 1, end);
// checks
if (x.contains("-")) {
x = x.replace("-", " ");
}
// if tag starts with a vowel, make "an";
if (vowelCheck(x) == true) {
preInput = "an";
}
System.out.println("please enter " + preInput + " " + x + ":");
String lib = userInput.nextLine();
// maybe make it not word
word = word.replace(x, lib);
// edit not holding changed text ,lib is correct though
output.print(word);
} else {
// normal words in lines with tags
output.print(word + " ");
}
}
I used some print statements to check var values and word = word.replace(x, lib) doesn't seem to be getting updated from the x value as I expected it to, Think that might be where the issue is occurring but haven't been able to get passed that.
I spent a long time trying to figure this out and I'm totally stuck! All help is greatly appreciated
data:
input file looks something like this:
<Male-Name> has announced that his <adjective>
clothing store in the heart of downtown <CITY> is having
a/an <adjective> sale of all merchandise, including
<unusual-adjective> suits and slightly irregular <plural-noun>
available. Men's cable-knit <plural-noun> , only $15.99.
Hand-woven Italian <plural-noun> , 1/2-price. Double-
breasted cashmere <plural-noun> , $50.00. Genuine imported
<Color!> <adjective> shoes, <Exciting-adjective> handerchiefs,
and women's embroidered <plural-noun> , all at rock-bottom prices.
This is a chance to get some really <Interesting-Adjective> bargains.
BAD OUTPUT:
<Male-Name>has announced that his <flat>
clothing store in the heart of downtown <LA>is having
a/an <fast>sale of all merchandise, including
<unusual-adjective>suits and slightly irregular <plural-noun>
available. Men's cable-knit <plural-noun>, only $15.99.
Hand-woven Italian <plural-noun>, 1/2-price. Double-
breasted cashmere <plural-noun>, $50.00. Genuine imported
<blue><small>shoes, <Exciting-adjective>handerchiefs,
and women's embroidered <plural-noun>, all at rock-bottom prices.
This is a chance to get some really <Interesting-Adjective>bargains.
All should be replaced just like the rest.
Your problem is that when you have dashes in a replacement tag, you're replacing the dashes with spaces, but then you're trying to find that modified version of the string in the input text to replace it with the user's input value. Since you changed the value after you pulled it out of the text, it is no longer going to match the text.
What you can do is save off the original value and use that to find/replace the user's input into the original text.
int start = word.indexOf("<");
int end = word.indexOf(">");
// word between <>
String x = word.substring(start + 1, end);
String origX = x; // <- MAKE A COPY OF THE TAG STRING
// checks
if (x.contains("-")) {
x = x.replace("-", " ");
}
String preInput = "";
System.out.println("please enter " + preInput + " " + x + ":");
String lib = userInput.nextLine();
// maybe make it not word
word = word.replace(origX, lib); // <- USE THE COPY YOU MADE EARLIER TO DO THE FIND/REPLACE
// edit not holding changed text ,lib is correct though
System.out.println(word);
Basically, a problem that I went into has many solutions but the only one that came to my mind is dirty and will end up with a lot of boilerplate code.
Let's say we have a log file with lines like this.
127.0.0.1 Amigo 30.08.2012 16:08:13 LOGIN OK <br>
192.168.100.2 Pete Tyson 30.08.2012 16:08:40 COMPLETE_TASK 15 OK <br>
146.34.15.5 Eduard Bentley 03.01.2014 03:45:23 LOGIN OK
What is the best solution if log file can dynamically change and not every line has same length and I would want to take username out of it?
Edit:
private int atWhatPlaceDateIs(String[] line) {
if(line[4].contains(":") && line[3].contains("."))
return 1;
else if (line[3].contains(":") && line[2].contains("."))
return 0;
return -1;
}
if(atWhatPlaceDateIs(line) == 1)
date = line[3] + " " + line[4];
else if(atWhatPlaceDateIs(line) == 0)
date = line[2] + " " + line[3];
That's what I did with date, it always is at 2-3 or 3-4 index
You should consider the use of a regular expression. There are tons of tutorials on the web to accomplish what you describe.
This tutorial here should provide you with enough to get started.
I am trying to write to a text document with a specific format. Here's what I have right now.
String line = "";
double totalCost = 0;
Node curr = summary.head.next;
while(curr!=summary.tail)
{
line += [an assortment of strings and variables] +"\r";
totalCost += PRICELIST.get(curr.itemName)*curr.count;
curr = curr.next;
}
write.printf("%s" + "%n", line);
This is what the part adding onto line actually looks like.
"Item's name: " + curr.itemName + ", Cost per item: " + NumberFormat.getCurrencyInstance().format(PRICELIST.get(curr.itemName)) +
", Quantity: " + curr.count + ", Cost: " + NumberFormat.getCurrencyInstance().format(PRICELIST.get(curr.itemName)*curr.count) + "\r";
I've tried that with a newline character too. Before I had it working when the print statement was inside the loop meaning it only wrote one line at a time. I want to do it this way because I will have multiple threads writing to this file and this way any thread will not hold the lock for as long.
If using Java 7 or later you can use System.lineSeparator()
Use System.getProperty("line.separator") instead of "\r"
Cache ir for efficiency though.
First of all don't use
while(..){
result += newString
..
}
inside loop. This is very inefficient especially for long texts because each time you call
result += newString
you are creating new String which needs to copy content of result and append to it newStrint. So the more text you processed so far, the more it has to copy so it becomes slower.
Instead use
StringBuilder sb = new StringBuilder();
while(..){
sb.append(newString);
}
result = sb.toString.
which in your case should be something more like
sb.append("Item's name: ").append(curr.itemName)
.append(", Cost per item: ").append(NumberFormat.getCurrencyInstance().format(PRICELIST.get(curr.itemName)))
.append(", Quantity: ").append(curr.count )
.append(", Cost: ").append(NumberFormat.getCurrencyInstance().format(PRICELIST.get(curr.itemName) * curr.count))
.append(System.lineSeparator());
Also instead of
write.printf("%s" + "%n", line);
you should use simpler version, which is
write.println(line);
which automatically add line separator based on OS.
You can also try to use \n\r in combination. This helped in one of my projects.
I am using a while loop and getting data from a text file and using classes to reference each string. I don't have any issues getting the values for each string and printing it out.
However, I am confused on how to use System.out.printf(....) to put all of the strings I need in one line while using a loop.
For example, let's say the text file was:
I
like
to
use
computers
I want to use a loop to print out the words into one string and I may have different spacing between each word.
The code I have so far:
while (!readyOrder.isEmpty()) {
s = readyOrder.poll();
System.out.printf(s.getQuantity() + " x " + s.getName()
+ "(" + s.getType() + ")" + " "
+ s.getPrice() * s.getQuantity());
System.out.println(" ");
total = total + s.getPrice() * s.getQuantity();
}
And the output should be:
1_x_The Shawshank Redemption_______(DVD)________________19.95
The underlined spaces are where the spaces should be and how long they should be.
How can I use printf to do that?
I think you need to use the string padding functionality of printf. For example %-30s formats to width of 30 characters, - means left justify.
for (Stock s : Arrays.asList(
new Stock(1, "The Shawshank Redemption", 100, "DVD"),
new Stock(2, "Human Centipede", 123, "VHS"),
new Stock(1, "Sharknado 2", 123, "Blu ray"))) {
System.out.printf("%2d x %-30s (%-7s) %5.2f\n",
s.getQuantity(), s.getName(), s.getType(),
s.getPrice() * s.getQuantity());
}
Output
1 x The Shawshank Redemption (DVD ) 100.00
2 x Human Centipede (VHS ) 246.00
1 x Sharknado 2 (Blu ray) 123.00
I am making an application where I will be fetching tweets and storing them in a database. I will have a column for the complete text of the tweet and another where only the words of the tweet will remain (I need the words to calculate which words were most used later).
How I currently do it is by using 6 different .replaceAll() functions which some of them might be triggered twice. For example I will have a for loop to remove every "hashtag" using replaceAll().
The problem is that I will be editing as many as thousands of tweets that I fetch every few minutes and I think that the way I am doing it will not be too efficient.
What my requirements are in this order (also written in comments down bellow):
Delete all usernames mentioned
Delete all RT (retweets flags)
Delete all hashtags mentioned
Replace all break lines with spaces
Replace all double spaces with single spaces
Delete all special characters except spaces
Here is a Short and Compilable Example:
public class StringTest {
public static void main(String args[]) {
String text = "RT #AshStewart09: Vote for Lady Gaga for \"Best Fans\""
+ " at iHeart Awards\n"
+ "\n"
+ "RT!!\n"
+ "\n"
+ "My vote for #FanArmy goes to #LittleMonsters #iHeartAwards"
+ " htt…";
String[] hashtags = {"#FanArmy", "#LittleMonsters", "#iHeartAwards"};
System.out.println("Before: " + text + "\n");
// Delete all usernames mentioned (may run multiple times)
text = text.replaceAll("#AshStewart09", "");
System.out.println("First Phase: " + text + "\n");
// Delete all RT (retweets flags)
text = text.replaceAll("RT", "");
System.out.println("Second Phase: " + text + "\n");
// Delete all hashtags mentioned
for (String hashtag : hashtags) {
text = text.replaceAll(hashtag, "");
}
System.out.println("Third Phase: " + text + "\n");
// Replace all break lines with spaces
text = text.replaceAll("\n", " ");
System.out.println("Fourth Phase: " + text + "\n");
// Replace all double spaces with single spaces
text = text.replaceAll(" +", " ");
System.out.println("Fifth Phase: " + text + "\n");
// Delete all special characters except spaces
text = text.replaceAll("[^a-zA-Z0-9 ]+", "").trim();
System.out.println("Finaly: " + text);
}
}
Relying on replaceAll is probably the biggest performance killer as it compiles the regex again and again. The use of regexes for everything is probably the second most significant problem.
Assuming all usernames start with #, I'd replace
// Delete all usernames mentioned (may run multiple times)
text = text.replaceAll("#AshStewart09", "");
by a loop copying everything until it founds a #, then checking if the following chars match any of the listed usernames and possibly skipping them. For this lookup you could use a trie. A simpler method would be a replaceAll-like loop for the regex #\w+ together with a HashMap lookup.
// Delete all RT (retweets flags)
text = text.replaceAll("RT", "");
Here,
private static final Pattern RT_PATTERN = Pattern.compile("RT");
is a sure win. All the following parts could be handled similarly. Instead of
// Delete all special characters except spaces
text = text.replaceAll("[^a-zA-Z0-9 ]+", "").trim();
you could use Guava's CharMatcher. The method removeFrom does exactly what you did, but collapseFrom or trimAndCollapseFrom might be better.
According to the now closed question, it all boils down to
tweet = tweet.replaceAll("#\\w+|#\\w+|\\bRT\\b", "")
.replaceAll("\n", " ")
.replaceAll("[^\\p{L}\\p{N} ]+", " ")
.replaceAll(" +", " ")
.trim();
The second line seems to be redundant as the third one does remove \n too. Changing the first line's replacement to " " doesn't change the outcome an allows to aggregate the replacements.
tweet = tweet.replaceAll("#\\w*|#\\w*|\\bRT\\b|[^##\\p{L}\\p{N} ]+", " ")
.replaceAll(" +", " ")
.trim();
I've changed the usernames and hashtags part to eating also lone # or #, so that it doesn't need to be consumed by the special chars part. This is necessary for corrent processing of strings like !#AshStewart09.
For maximum performance, you surely need a precompiled pattern. I'd also re-suggest to use Guava's CharMatcher for the second part. Guava is huge (2 MB I guess), but you surely find more useful things there. So in the end you can get
private static final Pattern PATTERN =
Pattern.compile("#\\w*|#\\w*|\\bRT\\b|[^##\\p{L}\\p{N} ]+");
private static final CharMatcher CHAR_MATCHER = CharMacher.is(" ");
tweet = PATTERN.matcher(tweet).replaceAll(" ");
tweet = CHAR_MATCHER.trimAndCollapseFrom(tweet, " ");
You can inline all of the things that are being replaced with nothing into one call to replace all and everything that is replaced with a space into one call like so (also using a regex to find the hashtags and usernames as this seems easier):
text = text.replaceAll("#\w+|#\w+|RT", "");
text = text.replaceAll("\n| +", " ");
text = text.replaceAll("[^a-zA-Z0-9 ]+", "").trim();