How to write this regex code snippet right? - ios

I am trying to get a UIWebView to display some text with images.
The text has some links inside of it so for example:
"I once had a fish http://mysite.com/images/fish.jpg.
I also owned a little dog and a rooster http://mysite.com/images/dog.jpg"
would result in:
I once had a fish
----------------------------------
| |
|Fish Image From |
|http://mysite.com/images/fish.jpg|
| |
| |
----------------------------------
I also owned a little dog and a rooster
----------------------------------
| |
|dog Image From |
|http://mysite.com/images/dog.jpg|
| |
| |
----------------------------------
--------------------------------------
| |
|rooster Image From |
|http://mysite.com/images/rooster.jpg|
| |
| |
--------------------------------------
NSMutableString *mutableString = [[NSMutableString alloc] initWithFormat:#"<html><head></head><body>%#</body>",string];
NSString *pattern = #"http://.+\\.(?:jpg|jpeg|png|gif|bmp)";
NSString *replacement = #"<br /><img style=\"width:100%;height:auto;\" src=\"$1\"/><br />";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern
options:0 error:NULL];
[regex replaceMatchesInString:mutableString
options:0
range:NSMakeRange(0, mutableString.length)
withTemplate:replacement];
But when I check on the result the image gets this: <br /><img style="width:100%;height:auto;" src=""/><br /> and the link is gone.
Where the image has to be :
Text...
<br /><img style="width:100%;height:auto;" src="http://mysite.com/images/fish.jpg"/><br />
Text...
<br /><img style="width:100%;height:auto;" src="http://mysite.com/images/dog.jpg"/><br />
<br /><img style="width:100%;height:auto;" src="http://mysite.com/images/rooster.jpg"/><br />

I think for $1 to work properly you need to put the matched pattern inside a group, i.e., inside (). How about using the following pattern:
(http://.+\\.(?:jpg|jpeg|png|gif|bmp))
Also from the pattern the .+ part my eat the whole string. To make it less greedy it can be replaced with .+? or more correctly \w.*? to keep the effect of + in the original pattern.

Related

Google sheets wrap cell content in other content

Im looking to create a formula in one column that takes the content from the adjacent column and wraps it inside some other content, can anyone help with this?
For example, given:
A | B
1| | someText1
2| | someText2
3| | someText3
4| | someText4
expected outcome content for Col A, after applying appropriate formula:
A | B
1| wrap("someText1") | someText1
2| wrap("someText2") | someText2
3| wrap("someText3") | someText3
4| wrap("someText4") | someText4
I hope this makes sense, any help would be appreciated. Thanks
What i ended up doing: Add a function and applied it to the whole column A
function getAdjacentValue() {
var range = SpreadsheetApp.getActiveRange();
var col = range.getColumn();
var row = range.getRow();
var range2 = SpreadsheetApp.getActiveSheet().getRange(row,col+1);
return 'wrap("'+range2.getValue()+'")';
}
By combining MewX suggestion with arrayformula one can achieve the same for all column, with one formula:
=arrayformula("wrap(""" & B1:B4 & """)")
Explanation: & is the string concatenation operator, quote marks within a string are escaped by doubling them.

JavaCC: treat white space like <OR>

I'm trying to build a simple grammar for Search Engine query.
I've got this so far -
options {
STATIC=false;
MULTI=true;
VISITOR=true;
}
PARSER_BEGIN(SearchParser)
package com.syncplicity.searchservice.infrastructure.parser;
public class SearchParser {}
PARSER_END(SearchParser)
SKIP :
{
" "
| "\t"
| "\n"
| "\r"
}
<*> TOKEN : {
<#_TERM_CHAR: ~[ " ", "\t", "\n", "\r", "!", "(", ")", "\"", "\\", "/" ] >
| <#_QUOTED_CHAR: ~["\""] >
| <#_WHITESPACE: ( " " | "\t" | "\n" | "\r" | "\u3000") >
}
TOKEN :
{
<AND: "AND">
| <OR: "OR">
| <NOT: ("NOT" | "!")>
| <LBRACKET: "(">
| <RBRACKET: ")">
| <TERM: (<_TERM_CHAR>)+ >
| <QUOTED: "\"" (<_QUOTED_CHAR>)+ "\"">
}
/** Main production. */
ASTQuery query() #Query: {}
{
subQuery()
( <AND> subQuery() #LogicalAnd
| <OR> subQuery() #LogicalOr
| <NOT> subQuery() #LogicalNot
)*
{
return jjtThis;
}
}
void subQuery() #void: {}
{
<LBRACKET> query() <RBRACKET> | term() | quoted()
}
void term() #Term:
{
Token t;
}
{
(
t=<TERM>
)
{
jjtThis.value = t.image;
}
}
void quoted() #Quoted:
{
Token t;
}
{
(
t=<QUOTED>
)
{
jjtThis.value = t.image;
}
}
Looks like it works as I wanted to, e.g it can handle AND, OR, NOT/!, single terms and quoted text.
However I can't force it to handle whitespaces between terms as OR operator. E.g hello world should be treated as hello OR world
I've tried all obvious solutions, like <OR: ("OR" | " ")>, removing " " from SKIP, etc. But it still doesn't work.
Perhaps you don't want whitespace treated as an OR, perhaps you want the OR keyword to be optional. In that case you can use a grammar like this
query --> subquery (<AND> subquery | (<OR>)? subquery | <NOT> subquery)*
However this grammar treat NOT as an infix operator. Also it doesn't reflect precedence. Usually NOT has precedence over AND and AND over OR. Also your main production should look for an EOF. For that you can try
query --> query0 <EOF>
query0 --> query1 ((<OR>)? query1)*
query1 --> query2 (<AND> query2)*
query2 --> <NOT> query2 | subquery
subquery --> <LBRACKET> query0 <RBRACKET> | <TERM> | <QUOTED>
Ok. Suppose you actually do want to require that any missing ORs be replaced by at least one space. Or to put it another way, if there is one or more white spaces where an OR would be permitted, then that white space is considered to be an OR.
As in my other solution, I'll treat NOT as a unary operator and give NOT precedence over AND and AND precedence over either sort of OR.
Change
SKIP : { " " | "\t" | "\n" | "\r" }
to
TOKEN : {<WS : " " | "\t" | "\n" | "\r" > }
Now use a grammar like this
query() --> query0() ows() <EOF>
query0() --> query1()
( LOOKAHEAD( ows() <OR> | ws() (<NOT> | <LBRACKET> | <TERM> | <QUOTED>) )
( ows() (<OR>)?
query1()
)*
query1() --> query2() (LOOKAHEAD(ows() <AND>) ows() <AND> query2())*
query2() --> ows() (<NOT> query2() | subquery())
subquery() --> <LBRACKET> query0() ows() <RBRACKET> | <TERM> | <QUOTED>
ows() --> (<WS>)*
ws() --> (<WS>)+

Use split in as delimiter

I have many .txt file in one location. This txt content is as below.
%-ile | Read (ms) | Write (ms) | Total (ms)
----------------------------------------------
min | N/A | 0.018 | 0.018
25th | N/A | 0.055 | 0.055
50th | N/A | 0.059 | 0.059
75th | N/A | 0.062 | 0.062
90th | N/A | 0.070 | 0.070
95th | N/A | 0.073 | 0.073
99th | N/A | 0.094 | 0.094
3-nines | N/A | 0.959 | 0.959
4-nines | N/A | 67.552 | 67.552
5-nines | N/A | 75.349 | 75.349
6-nines | N/A | 84.994 | 84.994
7-nines | N/A | 85.632 | 85.632
I am reading 3-nines from above content and want to write a program like it Total (ms) column's value in greater than 1 with respect to 3-nines row it should print that file name.
For that I have written a program as below:
$data = get-content "*.txt" | Select-String -Pattern "3-nines"
$data | foreach {
$items = $_.split("|")
if ($items[0] -ge 1 ) {Echo $items[1]}
}
But getting below error.
Method invocation failed because [Microsoft.PowerShell.Commands.MatchInfo] doesn't contain a method named 'split'.
At line:2 char:18
+ $items = $_.split <<<< ("|")
+ CategoryInfo : InvalidOperation: (split:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Cannot index into a null array.
At line:3 char:12
+ if ($items[ <<<< 0] -lt 1 ) {Echo $items[1]}
+ CategoryInfo : InvalidOperation: (0:Int32) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
Could you please help here. I am very new to the powershell scripting.
Change
$items = $_.split("|")
to:
$items = ([string]$_).split("|")
The contents of the match is returned as an array and it doesn't have a split method. Casting it to a string will give you the split method.
Update: To print the filename you have to change the script a bit since the current input for Select-String is an array so you loose the filename:
Select-String -Pattern "3-nines" -Path "*.txt" | foreach {
$items = ([string]$_).split("|")
if ([double]$items[3] -ge 1 ) {
Write-Output "FileName: $($_.Filename)"
Echo $items[3]
}
}
First of all - why would you pipe to Select-String here? You can use -Path parameter and pass *.txt directly to it.
The reason split doesn't work is because you should call it agains Line property of [Microsoft.PowerShell.Commands.MatchInfo] object. I guess what you need there is a simple Where-Object:
Select-String -Pattern 3-nines -Path *.txt |
Where-Object { [double]($_.line.Split('|')[-1]) -gt 1 } |
Select-Object Path, Line
Alternatively, you can turn content of the file into objects with Import-Csv cmdlet:
foreach ($file in Get-ChildItem -Path *.txt) {
# Existing headers are terrible - replacing them...
$3nines = Import-Csv -Path $file.FullName -Delimiter '|' -Header Percent, Read, Write, Total |
Where-Object Percent -match 3-nines
if ([double]$3nines.Total -gt 1) {
$3nines | Select-Object *, #{
Name = 'Path'
Expression = { $file.FullName }
}
}
}

Image Stretching Only some Part

I want to image stretching some portion of image. Just below cutout off image. But when text increase it stretch full image. But i want to stretch a image after so.link
balloonView.image =[[UIImage imageNamed:#"chatbg.png"] stretchableImageWithLeftCapWidth:50 topCapHeight:0];;
Please check http://postimg.org/image/7d7lzu9n1/f99d1ebd/
General Discussion : In order to create a stretchable UIImage in iOS like 9Patches in android, you can use this:
UIImage * backgroundImg = [UIImage imageNamed:#"bg.png"];
backgroundImg = [backgroundImg resizableImageWithCapInsets:UIEdgeInsetsMake(2,2, 2, 2)];
[imgView setImage:cellBackgroundImg];
you could also use:
- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth
topCapHeight:(NSInteger)topCapHeight
basically the image is not stretched in the area leftCapWidth pixels from the left and right edge and topCapHeight pixels from the top and the bottom. When the image is scaled the area inside of these limits is subject to stretching.
OP's issue : I think why you are not able to make it work is because your topCapHeight is set to 0. try something like this :
[[UIImage imageNamed:#"chatbg.png"] stretchableImageWithLeftCapWidth:50
topCapHeight:50];
A stretchable image is divided into 9 parts, if both leftCapWidth and topCapHeight are nonzero.
leftCapWidth
<----->
+--------------+ ^
| | | | |
| A | | B | | topCapHeight
|-----+·+------| v
|-----+·+------|
| C | | D |
| | | |
+--------------+
The central parts are always 1 px in size, and this is the part that is stretched, for example:
leftCapWidth (constant!)
<----->
+------------------+ ^
| | | | |
| A | | B | | topCapHeight (constant!)
v |-----+ - - +------| v
| | . . |
| | . . |
^ |-----+ - - +------|
| C | | D |
| | | |
+------------------+
>-----<
stretched region
Just use this code. If you have still any issue for left side arrow, then change the value of LeftCapWidth like 5,10,20 and then check it.
balloonView.image =[[UIImage imageNamed:#"chatbg.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:20];
Hope, this is what you're looking for. Any concern get back to me.

Misinterpreted grammar

I have the following grammar piece:
SlotConstraint:
lExpr = [Slot] pred = ('in' | 'inn' | 'from' | 'fromm' | 'is') rExpr = SetSexpr |
lExpr = [Slot] pred = ('in' | 'inn' | 'from' | 'fromm' | 'is')? neg = ('not' | 'not in' | 'not from') rExpr = SetSexpr
;
When I write something like this - a in b or a is not in b it is fine. However I am not able to write a is not b. The question is: why it understands not in or not from but not plain not?
Thanks
do not use whitespace in keywords

Resources