preg_replace troubles - preg-replace

I am struggling with this regular expression.
$glossary_search[] = "/(^|>|\\s)".$glossary["glossary_name"]."($|<|\\s)/i";
$glossary_replace[] = "\$1<a href='/jargon-buster/".tapestry_hyphenate($glossary["glossary_name"]).".html' title='".$glossary["glossary_name"]."' target='_blank'>".$glossary["glossary_name"]."</a>\$2";
return preg_replace($glossary_search,$glossary_replace,$text);
I am trying to replace words in a product description with a hyperlink. The code above works if the word has a space either side but does not work if it has a full stop, comma or "<". Can anyone spot my mistake?
Thanks,
Simon

I think you might need to use preg_quote and htmlentities?
$glossary_search[] = "/(^|>|\\s)".preg_quote(htmlentities($glossary["glossary_name"],ENT_COMPAT,'UTF8'))."($|<|\\s)/i";
$glossary_replace[] = "\$1<a href='/jargon-buster/".tapestry_hyphenate($glossary["glossary_name"]).".html' title='".$glossary["glossary_name"]."' target='_blank'>".$glossary["glossary_name"]."</a>\$2";
return preg_replace($glossary_search,$glossary_replace,$text);

Related

How can I manipulate a string in dart?

Currently I'm working in a project with flutter, but I realize there is a need in the management of the variables I'm using.
Basically I want to delete the last character of a string I'm concatenating, something like this:
string varString = 'My text'
And with the help of some method or function, the result I get:
'My tex'
Am I clear about it? I'm looking for some way which helps me to 'pop' the last character of a text (like pop function in javascript)
Is there something like that? I search in the Dart docs, but I didn't find anything about it.
Thank you in advance.
You can take a substring, like this:
string.substring(0, string.length - 1)
If you need the last character before popping, you can do this:
string[string.length - 1]
Strings in dart are immutable, so the only way to do the operation you are describing is by constructing a new instance of a string, as described above.
var str = 'My text';
var newStr = (str.split('')..removeLast()).join();
print(newStr);
Another way:
var newStr2 = str.replaceFirst(RegExp(r'.$') , '');
print(newStr2);

What ending marks should be used to extend a range to the end of the paragraph?

I am coding a word add-in and am not clear how to use the getNextTextRange(endingMarks, trimSpacing) method of the Range class.
Specifically I want to select a new Range starting from the currently selected range and going to the end of the paragraph.
The API for for the method states
endingMarks string[]
Required. The punctuation marks and/or other
ending marks as an array of strings
That's clear enough if you want to select up to the next comma, period or even space. But what ending marks should you use for a paragraph, a line break, or the end of the document?
I have tried using '\n', '^p' and '¶' but none of these seem to work.
var nr = selection.getNextTextRange(['¶'],true);
nr.load("isEmpty,text");
await context.sync();
console.log('nr='+nr.text);
} catch(e) {
console.log("error, soz");
console.log(e);
}
Given a document consisting of one paragraph of text with a blank paragraph after it, and the first word of the paragraph highlighted, this add-in throws a RichApi.Error
We couldn't find the item you requested.
I would expect it to instead print out the remainder of the paragraph.
If I understand your scenario, you can work with the ParagraphCollection.getFirst() method. Please install the Script Lab tool. Open the sample called "Get paragraph from insertion point" for an example.
Let me expand on rick-kirkham's answer in case it helps anyone else in my situation. This is basically the same answer as given here https://stackoverflow.com/a/51160690/4114053
Ok, here is my sample word document:
The rain in Spain falls. Mainly on the plain.
Alice stepped through the looking glass. What did she see?
And there endeth the lesson. Amen.
The user selects "stepped" in the second paragraph and I want to know what the text for the rest of the paragraph, from that word, says. I also want to know what the text up to that point says.
var doc = context.document;
var selection = doc.getSelection();
selection.load("isEmpty,text");
await context.sync();
console.log(selection.text); //prints stepped
var startRange = selection.getRange("start");
var endRange = selection.paragraphs.getLast().getRange("start");
var deltaRange = startRange.expandTo(endRange);
context.load(deltaRange);
await context.sync();
console.log(deltaRange.text); //prints "Alice"
startRange = selection.getRange("end");
endRange = selection.paragraphs.getLast().getRange("end");
deltaRange = startRange.expandTo(endRange);
context.load(deltaRange);
await context.sync();
console.log(deltaRange.text); // prints "through the looking glass. What did she see?"
My mistake was to get too caught up in trying to work out what "ending marks" might mean and how to use them to achieve this. (Although I still would like that spelled out in the API specification.)

Pyspark how to remove punctuation marks and make lowercase letters in Rdd?

I would like to remove punctuation mark and make the lowercase letters in RDD?
Below is my data set
l=sc.parallelize(["How are you","Hello\ then% you"\
,"I think he's fine+ COMING"])
I tried below function but I got an error message
punc='!"#$%&\'()*+,-./:;<=>?#[\\]^_`{|}~'
def lower_clean_str(x):
lowercased_str = x.lower()
clean_str = lowercased_str.translate(punc)
return clean_str
one_RDD = l.flatMap(lambda x: lower_clean_str(x).split())
one_RDD.collect()
But this gives me an error. What might be the problem? How can I fix this?
Thank you.
You are using the python translate function in a wrong way.
As I am not sure if you are using python 2.7 or python 3, I am suggesting an alternate approach.
The translate function changes a bit in python 3.
The following code will work irrespective of the python version.
def lower_clean_str(x):
punc='!"#$%&\'()*+,-./:;<=>?#[\\]^_`{|}~'
lowercased_str = x.lower()
for ch in punc:
lowercased_str = lowercased_str.replace(ch, '')
return lowercased_str
l=sc.parallelize(["How are you","Hello\ then% you","I think he's fine+ COMING"])
one_RDD = l.map(lower_clean_str)
one_RDD.collect()
Output :
['how are you', 'hello then you', 'i think hes fine coming']

Powershell - $var.ToString(x,y) issue

for a user creation scrip in powershell i'm using textbox object to fill the information of the new user (family name, first name)
I return a value like that:
$TextlabelUsername.text = $Textbox1.text.ToString().Substring(0,5)
Which apply on a button click.
But using that methode if one of my string value is less then 5 caracters the script return an error that the string is not enough long.
Is there a way to select 5 or less caracters or an other method to process ?
Try this:
$str = $Textbox1.text.ToString()
$TextlabelUsername.text = $str.Substring(0, [math]::Min(5, $str.Length))
There is nothing wrong with Ansgar Wiecher's method. Here is an alternative though:
$TextLabelUserName.Text = $Textbox1.Text.ToString() -replace '(.{0,5}).*', '$1'

NSRegularExpression to remove white space

I need a little kickstart on regex on the iPhone. Actually I am dealing with UITextField.text. If the value of the text is empty and if the value already exist, I can able to deal it. But, if the value is simply white spaces, I do not want to use it. So, if the value is like " " or " folder", I want the value to be "" and "folder" respectively.
I planned to use NSRegularExpression to remove the white space and went through the documents. But it was little confusing. So, help me to come out of the problem of removing white space from the given string. Thank you in advance.
Edit: you need to trim string, so no regular expression is needed, simply use:
[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
The regexp would be:
/\s+/g
You would then replace that with ""
How you do that through iOS syntax for the replacement I don't know, but that's the regExp for it all :)
A more practical question is, How to trim and condense white-spaces,
let text: String? = " I don't know you ! " // expected result: "I don't know you!"
let charSet = NSCharacterSet.whitespaceAndNewlineCharacterSet()
if let trimmedText = text?.componentsSeparatedByCharactersInSet(charSet).filter({!$0.isEmpty}).joinWithSeparator(" ") {
print(trimmedText) // I don't know you!
}