Search and replace a variable word - actionscript-3

I'm looking for a program which can create a text block with a different variable string every time.
I've tried doing this in certain languages, but I'd rather have a text editor which can do this.
Example: A list of words are chosen to replace a variable in a piece of text, that piece of text is then reprinted for every word.
I like Ice cream.
Ice cream is great.
Don't eat too much Ice cream.
I like Banana.
Banana is great.
Don't eat too much Banana.
I like Apple.
Apple is great.
Don't eat too much Apple.
I tried doing this in a programming language (AS3) but it doesn`t support multi-line strings very well.
What I`m looking for is either a text editor program (for Windows) which can do this, or a AS3 code snippet which can do this. (Which supports multi-line without the need of manually having to put \n everywhere.)

Not sure what to suggest for the multi-line issue - that's just how it is and you have to add \n or <br /> (in HTML text boxes).
As for the replace, that's a straightforward process. Just set up some type of token that you can replace in the text, e.g.
var str:String = "I like {}.\n{} is great.\nDon't eat too much {}.";
Then you can do either:
str.split("{}").join("Banana");
Or:
str.replace(/\{\}/g, "Banana");

The String Class has three handy methods for working with patterns and strings. These three methods are also case sensitive, meaning uppercase and lowercase matter when searching.
match()
search()
replace()
var string1:String = "Hello World!";
var subString:String = "Hell";
trace(string1.match(subString));
trace(string1.search(subString));
trace(string1.replace(subString, "Jell"));
match() method will display the substring if it is found and null if not found. The search() method will give a value of zero( 0 ) if the method finds the substing, and a value of negative one ( -1) if not found. The replace() method will replace the target substring with a new substring if the substring is found. You can also make the value nothing to simply remove an unwanted part of a string.
we can run conditionals like this:
var string1:String = "Hello World!";
var subString:String = "Hell";
if (string1.search(subString) == 0) {
trace(subString + " is in the string, I can now replace it or remove it.");
} else {
trace(subString + " is not in this string.");
}

Related

What is regex expression for a string after I use nokogiri to scrape

I have this string and it is in an html document of 100 other names that are formatted the same:
<li>Physical education sed<span class="meta"><ul><li>15184745922</li></ul></span>
</li>
And I want to save 'Physical education sed under a name column and '15184745922' under a number column.
I was wondering how do you do this in Ruby.
In nokogiri I can get only the li's by doing this:
puts page.css("ul li").text
but then it comes out all in one word:"Physical education sed15184745922"
I was thinking regex is the way to go but I am stumped with that.
I did split it on the li
full_contact = page.css("ul li")[22]
split_contact_on_li = full_contact.to_s.split(/(\W|^)li(\W|$)/).map(&:to_sym)
puts split_contact_on_li
and I get this
<
>
Physical education sed<span class="meta"><ul>
<
>
15184745922<
/
>
</ul></span>
<
/
>
The same number of lines will be shown for each contact_info and the name is always the third line before the span class and the number is always the 6th line.
There is an instance where there might be an email address instead on the 6th line put not often.
So should I match the second and the third angular bracket and pull the information up to the third and fourth bracket then shove it into an array called name and number?
You shouldn't use a regex to parse xhtml since the regex engine might mess up things, you should use a html parser instead. However, if you want to use a regex, you can use a regex like this:
<li>(.*?)<.*?<li>(.*?)<
Working demo
The idea behind this regex is to use capturing groups (using paretheses) to capture the content you want. So, for you sample input the match information is:
MATCH 1
Group 1. [4-26] `Physical education sed`
Group 2. [53-64] `15184745922`
For example;
#!/usr/bin/env ruby
string = "<li>Physical education sed<span class=\"meta\"><ul><li>15184745922</li></ul></span></li>"
one, two = string.match(/<li>(.*?)<.*?<li>(.*?)</i).captures
p one #=> "Physical education sed"
p two #=> "15184745922"
Why don't you just do a regex on the string "physical education sed15184745922"? You can match on the first digit, and get back the number and the preceding text.
I don't know how to use Ruby, but if I understand your question correctly I would take advantage of the gsub function (or Ruby's equivalent). It might not be the prettiest approach, but since we just want the text in one variable and the numbers in another, we can just replace the characters we don't want with empty values.
v1 = page.css('ul li').text
v2 = gsub('\d*', '', v1)
v3 = gsub('(^\d)', '', v1)
v1 gets the full text value, v2 replaces all numeric characters with '', and v3 replaces all alpha characters with '', giving us two new variables to put wherever we please.
Again, I don't know how to use Ruby, but in R I know that I could get all of the values from the page using the xpath you provided ("ul li") into a vector and then loop across the vector performing the above steps on each element. I'm not sure if that adequately answers your question, but hopefully the gsub function gets you closer to what you want.
You need to use your HTML parser (Nokogiri) and regular expressions together. First, use Nokogiri to traverse down to the first parent node that contains all the text you need, then regex the text to get what you need.
Also, consider using .xpath instead of .css it provides much more functionality to search and scrape for just what you want. Given your example, you could do like so:
page.xpath("//span[#class='meta']/parent::li").map do |i|
i.text.scan(/^([a-z\s]+)(\d+)$/i).flatten
end
#=> [['Physical education sed', '15184745922'], ['the next string', '1234567890'], ...]
And now you have a two-dimensional array you can iterate over and save each pair.
This bit of xpath business: "//span[#class='meta']/parent::li" is doing what .css can't do, returning the parent node that has the text and specific children nodes you want to scrape against.

Change CAPS lock to Capitalize in CSS

I have a sentence coming in that is all in CAPs lock (and can't be changed). That sentence is part of a paragraph, using CSS only (or a little Jquery if you have to). How can I always get the result reliably and across most devices!
Note: I have looked at questions such as this, but they do not cover the multiple sentences factor.
Without change:
THIS IS THE FIRST SENTENCE. And this is the second, as it is from the server.
Desired result:
This is the first sentence. And this is the second...
The CSS I tried was this, but it doesn't work with multiple sentences.
p { text-transform: lowercase;}
p:first-letter {text-transform:capitalize}
Seems like a problem for jQuery. Check this answer for the entire-element capitalization, then you can parse the first sentence by using something like:
var setval = $('#my_paragraph').html();
var firstSentence = setval.substring(0, setval.indexOf('.'));
firstSentence = toProperCase(firstSentence);
var theRest = setval.substring(setval.indexOf('.') + 1);
$('#my_paragraph').html(firstSentence + theRest);
This only a hotfix. If your output ever changes to something different, containing more then only a single dot or even other words starting with an uppercase character, this code will not provide the desired result.
Example:
http://jsfiddle.net/Em2bD/
// grab your text
var firstSentenceText = $('p').text();
// extract the first sentence and make it all lowercase
var firstSentence = firstSentenceText.substr(0, firstSentenceText.indexOf('.')).toLowerCase();
// convert first char to uppercase
var result = firstSentenceText.charAt(0).toUpperCase() + firstSentence.substring(1);
// append the text to what ever you like and append the missing dot.
$('.result').text(result + '.');
Something that comes in mind is by using a bit of jquery. You can find the first period(.) in the paragraph and then you can make the string before it lowercase(you can give it a span with a class/id and have the rules already on css file). You may have to do a bit of googling.

JTextPane highlighting based on regex?

I would like to highlight words matching a regular expression in a JTextPane.
I've seen various examples but they all where very complicated with complete syntax highlighting. I just want to highlight (or set in bold) a word/sentence, is there a simple way to do that?
If the overall underlying text of what is in the JTextPane is just a regular string and not HTML:
Get the underlying document from the JTextPane.
StyledDocument sdoc = textpane.getStyledDocument()
EDITED: changed to directly calling textPane.getStyledDocument, instead of casting the result of getDocument()
Get the text of the document.
String text = sdoc.getText(0, sdoc.getLength())
Use the Pattern and Matcher classes to find the locations that match the regular expression. I assume you already know how to do that.
For every location where a match is found, highlight the start to end of the matching substring with sdoc.setCharacterAttributes or use a highlighter (see JTextPane highlight text)

Cleaning up text: from ALLCAPS to <em>allcaps</em>

I need to clean up some text for html that used ALLCAPS instead of italics. So I'd like to take something that looks like this:
Here is an artificial EXAMPLE of a piece of TEXT that
uses allcaps as a way of EMPHASIZING words.
And convert it into this:
Here is an artificial <em>example</em> of a piece of <em>text</em> that
uses allcaps as a way of <em>emphasizing</em> words.
I'm tagging this with regex and notepad++, but (as you can probably tell) I don't know the first thing about how to use them.
There're no such possibilities with Notepad++ regex engine.
You can run a script that do the job, in Perl for example:
perl -pi.back -e "s#\b([A-Z]+)\b#'<em>'.lc($1).'</em>'/eg" yourfile.html
yourfile.html will be saved in yourfile.html.back
As far as I konw the regex engine of Notepad++ is not advanced enough to do this.
I would advice to use a programming language to accomplish this, in PHP for example you could do this:
echo preg_replace_callback('/([A-Z]{2,})/', create_function('$s', 'return "<em>".strtolower($s[0])."</em>";'), $s);
Be sure to exclude the legitim first capital letter of a single word in the regex.
AFAIK you cannot change casing in the Find\Replace mechanism of Notepad++.
If all you need is the <em> tag insertion you can do the following:
In the Find box type (\s+)([A-Z]+)(\s+), abd in the Replace type \1<em>\2</em>\3.
You can try some of the TextFX tools maybe in the TextFX Characters sub-menu.
Here is how to do this using JavaScript's string replace method:
var capfix = function (x) {
var emout = function (y) {
y = y.charAt(0) + "<em>" + y.toLowerCase() + "</em>" + y.charAt(y.length - 1);
};
return x.replace(/\s[A-Z]\s/g, emout);
};
To execute just call:
capfix(yourData);
This assumes that "yourData" is just a variable that represents your data as a string. If you wanted to use a web tool then "yourData" could represent the value from some input control, as in the following:
var yourData = document.getElementById("myinput").value;
alert(capfix(yourData));
To make that work just put an id attribute on your web tool input such as:
<textarea id="myinput"></textarea>

Surrounding text with tag and populating tag

I have several lines of text, in them there is a word or words that are capitalized like this:
Hello HOW ARE YOU good to see you
I am FINE
Is there a tool that can go through the text and surround all those capitalized with the HTML anchor text?
and
I guess more difficultly, also populate the href with uncapitalized, space(s) removed version of that capitalized text?
Any help on one or both questions is appreciated.
It took me a while, but here it is in javascript: http://jsfiddle.net/RdJ4E/4/
I'm sure you will find the way hot to tune the code. Good luck!
Is this a beginning? Matching all uppercased words is trivial with regex, and with providing the String.replace method with a callback function instead of a string you can do whatever you want with the matched string.
myString.replace(/(\b[A-Z\s]+\b)/g, function(result, match){
var stripped = encodeURI(result.trim().toLowerCase());
return ' '+result.trim()+' ';
});
http://jsfiddle.net/mwxnC/2/