I'm trying to replace a character NOT AT THE START OF THE STRING, with itself followed by another character, using regexKitLite.
thisPlate = [sBasePlate
stringByReplacingOccurrencesOfRegex:[NSString stringWithFormat:#"([^\\^]%#)", thisChar]
withString:[NSString stringWithFormat:#"\1%#", thisRep]
];
If sBasePlate is "temp", then thisPlate gets set to emp, but I'm expecting it to be teemp
So I'm trying to replace NOT THE START OF THE STRING, followed by thisChar, with that which has been matched followed by thisRep.
Have I got my backreferences wrong? Because that's what seems to be missing. It's adding in thisRep, but ignoring the initial match and not putting it back in with \1
Sorry if I've done something really stupid and obvious, this is my first app.
Right, I solved it. I hate answering my own questions but someone else might make the same mistake as me, so the big stupid obvious thing I missed...
back references should be $ signs.
withString:[NSString stringWithFormat:#"$1%#", prevRep]
That's how that string should be written.
Annoyingly the documentation here says that a back reference should be "\n":
http://regexkit.sourceforge.net/RegexKitLite/
But an example elsewhere shows $n. I should have guessed that. Oh well.
Related
I have a UITextView in my Swift app in which users can input text. They can input text with as many line breaks as they like but I need to save the string with the newline command (\n). How would I do this?
For example, my user inputs
Line 1
Line 2
Line 3
in the UITextView. If I was to retrieve the string...
let string = textview.text!
this would return
"Line 1
Line 2
Line 3"
when I would like for it to return
"Line1\nLine2\nLine3"
How would I go about doing this? Can I use a form of replacingOccurrences(of:with:)? I feel like I'm missing a fairly obvious solution...
Eureka! After WAY too much research and learning all about String escapes, I found a very simple solution. I'm quite surprised that this isn't an answer out there already (as far as I can tell haha) so hopefully, this helps someone!
It's actually quite simple and this will work of any String you could be using.
textView.text!.replacingOccurrences(of: "\n", with: "\\n")
Explanation:
Ok so as you can tell, it's quite simple. We want to replace the newline command \n with the string "\n". The problem is that if we replace \n with \n, it's just going to transfer over to a newline, not a string. This is why escapes are so important. As you can see, I am replacing \n with \\n. By adding an extra \ we escape the command \n entirely which turns it into a string.
I hope this helps someone! Have a great day!
Have you tried replacing \r with \n? or even \r\n with \n?
I hope I am not making an obvious assumption you considered, but maybe this may come in handy:
Is a new line = \n OR \r\n?
So as in the title, I'm trying to format a date in dd_mm_yy format using time.Now().Format("02_01_2006") as shown in this playground session:
http://play.golang.org/p/alAj-OcRZt
First problem, dd_mm_yyyy isn't an acceptable format, only dd_mm_yy is, which is fine I can manipulate the returned string myself.
The problem I have for you is to help me figure out what Go is even trying to do with this input.
You should notice the result you get is:
10_1110009
A good few thousand years off and it's lost the underscore which it only does it for _2. Does this character sequence represent something special here?
Replacing the last underscore with a hyphen or space returns a valid result. dd_mm_yy works fine. Just this particular case seems to completely fly off the handle.
On my local machine (Go playground is on a specific date) the result for today (the 5th) is:
05_01 5016
Which is equally strange, if not moreso as it's substituted in a space which seems to be an ANSIC thing.
This is very likely due to the following bug: https://github.com/golang/go/issues/11334
This has been fixed in Go 1.6beta1
Found an issue from their github:
https://github.com/golang/go/issues/11334
Basically _2 is taking the 2 as the day value from the reference time and then trying to parse the rest (006) which it doesn't recognise so it all goes wrong from there.
I recently have seen an expression in Crystal. It's a formula, when I edit it, its content is something like this:
("sNumber") + ":"
However, when I print this report, the code above will become: Number:
I think ("sNumber") is something like a variable. But I cannot find where it is be declared. I searched a lot on web but I find nothing.
So my question is:
Where can I find it?
How can I edit its value?
Any help would be welcome!
UPDATE:
I tried some expression, and find out all string after "s" will be displayed on the report, and those before "s" will be removed.
Maybe it's just some string expression not in document. If someone knows the specification, please add below.
After some further test, I have to say it is not a variable. It's just that everything after the letter "s" will be printed.
But I can see the whole string "sNumber" in the preview view.
My conclusion is if I want to change it, just modify the string after "s".
try this in your Formula "(""sNumber"") :" if you just need to create it as string
I asked a question earlier today and got a really quick answer from llbrink. I really should have asked that question before I spent several hours trying to find an answer.
So - here's another question that I have never found an answer for (although I have created a work-around which seems very cludgy).
My AHK program asks the user for a login name. The program then compares the login name with an existing list of names in a file.
The login name in the file may contain spaces, but there are never spaces at the beginning of the name. When the user enters the name, he may include spaces at the beginning. This means that when my program compares the name with those in the file, it can not find a match (because of the extra spaces).
I want to find a way of stripping the spaces from the beginning of the input.
My work-round has been to split the input string into an array (which does ignore leading spaces) and then use the first element of the array. This is my code :
name := DoStrip(name)
DoStrip(xyz) ; strip leading and trailing spaces from string
{
StringSplit, out, xyz, `,, %A_Space%
Return out1
}
This seems to be a very laboured way to do it - is there a better way ?
I don't see a problem with your example if it works on all cases.
There is a much simpler way; just use Autotrim which works like this.
AutoTrim, On ; not required it is on by default
my_variable = %my_variable%
There are also many other different ways to trim string in autohotkey,
which you can combine into something useful.
You can also use #LTrim and #RTrim to remove white spaces at the beginning and at the end of the string.
I got stricky/old php code, I just try to clean it , fix some bugs, and so on. Also the server uses php 4 too.
The problem is the following:
I get some data back from the database, I work with those data and show them. If the result contains a dollar sign, the PHP try to handle it as a variable.
For example :
$result = $this->sqlresult('SELECT * From Tablename where id=15');
$details = $result['description'];
echo $details;
Let me show an example what's happening , when the $result['description'] contains any wrong text, like 'This book is available for $148':
It usually doesn't show anything or show a wrong text , like This book is available for 48.
I have tried a preg replace functions on the details, I was looking for char changes , or html_special_chars , and tried those too, but nothing happened or not the original text came up.
preg_replace('/\$ /','/$/;' $details);
I know , that the double quotes on passing variables causes a similar error. I checked this topic too, but it wasn't a solution for me.
Current solution is just adding an extra space between the price amount the $ sign, but I am looking for a better one.
preg_replace('/\$/','/\$ /' $details);
Have you tried to use escape characters? This book is \$148.