I've been doing this for a couple of days, and I'm having this problem:
Whenever I try to encode a map into query string, I get the error "syntax error before: chat_id"
form = %{
"chat_id" => 237799109,
"text" => "OMG a message"
}
{status, body} = URI.encode_query(form)
#=> (SyntaxError) lib/elixir.ex:20: syntax error before: chat_id
But as far as I know this is the map syntax, isn't it? As seen here, where this example is presented:
iex> hd = %{"foo" => 1, "bar" => 2}
iex> URI.encode_query(hd)
"bar=2&foo=1
What is happening here?
Full error message:
== Compilation error on file lib/elixir.ex ==
** (SyntaxError) lib/elixir.ex:20: syntax error before: chat_id
(elixir) lib/kernel/parallel_compiler.ex:117: anonymous fn/4 in Kernel.Paral
lelCompiler.spawn_compilers/1
I don't know why you would get the error you listed, but URI.encode_query/1 only returns a single binary argument. You are trying to pattern match it against a tuple.
Can you paste more of the code instead of just those 2 lines?
iex(2)> URI.encode_query(form)
"chat_id=237&text=OMG+a+message"
when i alert the array, he return the hole value, but when i try to get it from getElementById, he stop after the first space!??
I know, it's probably simple, but i dont understand the difference between the 2 answers
https://jsfiddle.net/prodeinfo/jvywho3y/
html
<div>
<div id="statusList"></div>
</div>
javascript
var countryLan = ["nothing","Also nothing",];;
alert(countryLan[1]);
document.getElementById('statusList').innerHTML += "<input type='text' value=" + countryLan[1] + " />";
Try this:
document.getElementById('statusList').innerHTML += "<input type='text' value='" + countryLan[1] + "' />";
as you can see, I added ' in value: value='" + countryLan[1] + "' />"
You had a problem because if you want to pass a text that contains spaces, you need to quote this text properly.
In your case, you just put the value from an array as is, without quotes and that's why there was only "Also" in your input.
Now it should work.
Good luck.
The following formats are allowed for the phone number
xxx-xxx-xxxx [x represents a digit]
xxx.xxx.xxxx
xxxxxxxxxx [digit ten times]
I have the working sample for the formats but I am unable to combine them in a single regex. How to combine them into a single regex?
"/^[1-9]\d{2}-\d{3}-\d{4}|^\d{10}$/"
"/^[1-9]\d{2}[.]\d{3}[.]\d{4}|^\d{10}$/"
"/^\d{10}$/"
My regex code in angular:
<div class="form-group" ng-class="{'has-error':userprofileForm.phone.$touched && userprofileForm.phone.$invalid && userprofileForm.extension.$touched && userprofileForm.extension.$invalid}">
<label for="profile-phone" class="control-label">{{'PHONE'|translate }}</label>
<div>
<input name="phone" type="text" class="form-control" ng-model="userprofile.phoneNumber" ng-pattern="/^\d{10}$/" required="required" />
<div ng-show="userprofileForm.phone.$touched && userprofileForm.phone.$invalid">
<span ng-message="required">Please enter phone number</span>
</div>
</div>
</div>
You can combine them like so:
ng-pattern="/^([1-9]\d{2}-\d{3}-\d{4})|([1-9]\d{2}\.\d{3}\.\d{4})|(\d{10})$/"
Just put every pattern in its own group with () and or them together with |.
Or more compact using a back reference (assuming your third case should also not start with a 0):
ng-pattern="/^[1-9]\d{2}([.-]?)\d{3}\1\d{4}$/"
RegEx breakdown:
^ // start of line
[1-9] // match '1', '2', '3', '4', '5', '6', '7', '8' or '9'
\d{2) // match 2 digits
( // begin capturing group 1
[.-] // match '.' or '-'
? // make the preceeding [.-] optional, so capturing group 1 matches '.', '-' or nothing.
) // end capturing group 1
\d{3) // match 3 digits
\1 // back reference: match what was matched by capturing group 1
\d{4) // match 4 digits
$ // match end of line
Note that due to the use of the back reference a mix like xxx.xxx-xxxx is correctly rejected.
Here's a similar post with various answers including $filter, regex etc.
Do check it out..
Also this handy online regex validator explains your regex might help validate the syntax.
Hope this helps.
So I am working in a site that sores cuneiform tablets info. We use semitic chars for transliteration.
In my script, I create a term list from the translittaration of a tablet.
My problem is that with the Š, my script created two different terms because it thinks there is a space in the word because of the way cake treats the special char.
Exemple :
Partial contents of a tablet :
utu-DIŠ-nu-il2
Terms from the tablet when treated by my script :
utu-DIŠ, -nu-il2
it should be :
utu-DIŠ-nu-il2
When I print the contents of my array in course of treatment of the contents, I see this :
utu-DI� -nu-il2
So this means the uncorrect parsing of the text creates a space that is interpreted in my script as 2 words instead of one.
In the database, the text is fine...
I also get these errors :
Warning (512): SQL Error: 1366: Incorrect string value: '\xC5' for column 'term' at row 1 [CORE\cake\libs\model\datasources\dbo_source.php, line 684]
Query: INSERT INTO terms (term, lft, rght) VALUES ('utu-DI�', 449, 450)
Query: INSERT INTO terms (term, lft, rght) VALUES ('A�', 449, 450)
Query: INSERT INTO terms (term, lft, rght) VALUES ('xDI�', 449, 450)
Anybody knows what I could do to make this work ?
Thanks !
Added info :
$terms=$this->data['Tablet']['translit'];
$terms= str_replace(array('\r\n', '\r', '\n','\n\r','\t'), ' ', $terms);
$terms = trim($terms, chr(173));
print_r($terms);
$terms = preg_replace('/\s+/', ' ', $terms);
$terms = explode(" ", $terms);
$terms=array_map('trim', $terms);
$anti_terms = array('#tablet','1.','2.','3.','4.','5.','6.','7.','7.','9.','10.','11.','12.','13.','14.','15.','16.','17.','18.','19.','20.','Rev.',
'Obv.','#tablet','#obverse','#reverse','C1','C2','C3','C4','C5','C6','C7','C8','C9', '\r', '\n','\r\n', '\t',''. ' ', null, chr(173), 'x', '[x]','[...]' );
foreach($terms as $key => $term) {
if(in_array($term, $anti_terms) || is_numeric($term)) {
unset($terms[$key]);
}
}
If I put my print_r before the preg, the S are good, if I do it after, they display with the black lozenge. So I guess the preg function is the problem !
just found this :
http://www.php.net/manual/fr/function.preg-replace.php#84385
But it seems that
mb_ereg_replace()
causes the same problem as preg_replace() ....
Solutuion :
mb_internal_encoding("UTF-8");
mb_regex_encoding("UTF-8");
$terms = mb_ereg_replace('\s+', ' ', $terms);
and error is gone ... !
mb_internal_encoding("UTF-8");
mb_regex_encoding("UTF-8");
$terms = mb_ereg_replace('\s+', ' ', $terms);
I am trying to construct a Control template from code behind. Things were working fine till recently I found that the code was throwing an exception because of escape characters in string. The error message is dynamically constructed by retrieving from resource file.
The exception is
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
Additional information: Name cannot begin with the '#' character, hexadecimal value 0x40. Line 1, position 537.
//In this case when exception is thrown,
//string errorMessage = "Name cannot contain any of the following characters $ \" # ; ^ | "
public static ControlTemplate GetErrorTemplate(string errorMessage)
{
string xamlString = "<ControlTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" " +
"xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" " +
"xmlns:nicefx=\"clr-namespace:NiceFx.Interop.UIComponents;assembly=NiceFx\" " +
"xmlns:wpfkit=\"http://schemas.microsoft.com/wpf/2008/toolkit\" >" +
" <DockPanel LastChildFill=\"True\">" +
"<TextBlock Foreground=\"White\" Background=\"Red\" FontSize=\"12\" Padding=\"2\" FontFamily=\"Trebuchet MS\" Margin=\"5,5,0,0\" TextWrapping=\"Wrap\" DockPanel.Dock=\"Bottom\" Text=\"" + errorMessage + "\"></TextBlock>" +
"<AdornedElementPlaceholder />" +
" </DockPanel>" +
" </ControlTemplate>";
//EXCEPTION OCCURS IN THIS LINE
ControlTemplate ct = (ControlTemplate)XamlReader.Load(XmlReader.Create(
new StringReader(xamlString)));
return ct;
}
How do I escape this string? I tried all possible ways but I am unable to do so.
According to the comment in your code, errorMessage contains a ", which will be inserted (without escaping it) into the XAML you are constructing. This " will then act as the closing quote of the Text attribute. At this point, the next non-whitespace character the parser encounters will be #, which is not an allowed character for the name of a XAML attribute, so it stops and reports the error.
That covers the why. As for how to escape it, you can use the XML entity for double quote: " Note that you may need to apply this escaping to multiple characters in your parameter.