node.js - Regex: Put space between triple mustaches ( {{{ => { {{ ) - node.js

I have a String which contains minified CSS and handlebars.js notations (if statements, variables and so on). Because that the String is minified, it also contains stuff like '{{{' or '}}}' which I need to replace with '{ {{' or '}} }' (put one space between) in order to compile it correctly through handlebars.
Trouble is that I cannot manage to put the correct Regex together for this simple task. I guess the { symbols make the whole thing difficult since its a Regex-specific character.
String:
.class1{{{#if style.textColor}}color:{{style.textColor}};{{/if}}}item-price{{{#if style.showPrice}}display:block;{{else}}display:none;{{/if}}{{#if style.fontSizeItemPrice}}font-size:{{style.fontSizeItemPrice}}px;{{/if}}}
Expected output:
.class1{ {{#if style.textColor}}color:{{style.textColor}};{{/if}} }item-price{ {{#if style.showPrice}}display:block;{{else}}display:none;{{/if}}{{#if style.fontSizeItemPrice}}font-size:{{style.fontSizeItemPrice}}px;{{/if}} }
Simply substituting triple mustaches works, but only for the first occurance:
css = css.replace("{{{", "{ {{");
css = css.replace("}}}", "}} }")

String.replace function automatically converts the first string param to regex (without the global option set). That's why only the first occurrence is replaced. If you want to replace all occurrences of the pattern, you can create a regex with global option set. Try the following snippet.
css = css.replace(/{{{/g, '{ {{').replace(/}}}/g, '}} }')

Related

How to use re.sub in Python

Please help me replace a particular string with re.sub()
'<a href="/abc-10063/" target="_blank">'
needs to be
'<a href="./abc-10063.html" target="_blank">'
Wrote a script below
import re
test = '<a href="/abcd-10063/" target="_blank">'
print(re.sub(r'/abcd-[0-9]','./abcd-[0-9].html', test))
which returns
<a href="./abcd-[0-9].html0063/" target="_blank">
First of all your regular expression is incorrect. It will match /abcd-1 only.
You need to change your regex to /abcd-[0-9]+. Adding a + will match all the numbers. Also to match the trailing /, you need to add that in your regex.
So final regex will be /abcd-[0-9]+/.
Now to reuse the matched content in substitution you need to create groups in your regex. Since we want to reuse just the /abcd-[0-9]+and not the /. Put /abcd-[0-9]+ in group, like this: (/abcd-[0-9]+)/.
Now we can use \1 to use matched group in the substitution, where 1 is the group number. If you wanted to use second group, you will use \2.
So your final code will be:
import re
test = '<a href="/abcd-10063/" target="_blank">'
print(re.sub(r'(/abcd-[0-9]+)/', r'.\1.html', test))

SPLIT results with separator

I'm trying to split a string (separated with the HTML break tag), without deleting the break tag. I think it's pretty messy to add a break as string after splitting, so is there any function/possibility to keep the separator while "splitting"?
Example:
<HTML><BODY><p>some text<br/>some more text</p></BODY></HTML>
Expected result:
<HTML><BODY><p>some text<br/>
some more text</p></BODY></HTML>
As far as I know SPLIT removes the separator from the results and it doesn't seem like you can change that.
But you could create your own separator by first replacing your <br/> tag with <br/> plus an arbitrary string that is highly unlikely to ever appear in your HTML source, and then split the HTML using this arbitrary string as a separator instead.
types:
begin of t_result,
segment(2000) type c,
end of t_result.
DATA:
source type string,
separator type string,
brtag type string,
repl type string,
result_tab type standard table of t_result,
result_row TYPE t_result.
brtag = '<br/>'.
separator = '|***SEP***|'.
concatenate brtag separator into repl.
source = '<HTML><BODY><p>some text<br/>some more text</p></BODY></HTML>'.
replace all occurrences of brtag in source with repl.
split source at separator into table result_tab.
LOOP AT result_tab INTO result_row.
WRITE:
result_row-segment.
ENDLOOP.
Output of that example report:
<HTML><BODY><p>some text<br/>
some more text</p></BODY></HTML>
The caveat of this solution is that your custom separator, if not chosen with some care, might appear in your HTML source on its own. I therefore would choose an arbitrary string with a special character or two that would be encoded in HTML (like umlauts) and therefore not appear in your source.
Just use the replace command. replace <br/> with <br/>CR_LF
The CR_LF refers to the carriage return linefeed character.
In more complex cases you can use regex expressions in abap.
class ZTEST_SO definition public create public .
public section.
methods t1.
ENDCLASS.
CLASS ZTEST_SO IMPLEMENTATION.
METHOD T1.
data: my_break type string,
my_string type string
value '<HTML><BODY><p>some text<br/>some more text</p></BODY></HTML>'.
my_break = '<br/>' && CL_ABAP_CHAR_UTILITIES=>CR_LF.
replace all occurrences of '<br/>' in my_string with my_break in character mode.
"check my_string in the debugger :)
"<HTML><BODY><p>some text<br/>
"some more text</p></BODY></HTML>
ENDMETHOD.
ENDCLASS.

Reading from a string using sscanf in Matlab

I'm trying to read a string in a specific format
RealSociedad
this is one example of string and what I want to extract is the name of the team.
I've tried something like this,
houseteam = sscanf(str, '%s');
but it does not work, why?
You can use regexprep like you did in your post above to do this for you. Even though your post says to use sscanf and from the comments in your post, you'd like to see this done using regexprep. You would have to do this using two nested regexprep calls, and you can retrieve the team name (i.e. RealSociedad) like so, given that str is in the format that you have provided:
str = 'RealSociedad';
houseteam = regexprep(regexprep(str, '^<a(.*)">', ''), '</a>$', '')
This looks very intimidating, but let's break this up. First, look at this statement:
regexprep(str, '^<a(.*)">', '')
How regexprep works is you specify the string you want to analyze, the pattern you are searching for, then what you want to replace this pattern with. The pattern we are looking for is:
^<a(.*)">
This says you are looking for patterns where the beginning of the string starts with a a<. After this, the (.*)"> is performing a greedy evaluation. This is saying that we want to find the longest sequence of characters until we reach the characters of ">. As such, what the regular expression will match is the following string:
<ahref="/teams/spain/real-sociedad-de-futbol/2028/">
We then replace this with a blank string. As such, the output of the first regexprep call will be this:
RealSociedad</a>
We want to get rid of the </a> string, and so we would make another regexprep call where we look for the </a> at the end of the string, then replace this with the blank string yet again. The pattern you are looking for is thus:
</a>$
The dollar sign ($) symbolizes that this pattern should appear at the end of the string. If we find such a pattern, we will replace it with the blank string. Therefore, what we get in the end is:
RealSociedad
Found a solution. So, %s stops when it finds a space.
str = regexprep(str, '<', ' <');
str = regexprep(str, '>', '> ');
houseteam = sscanf(str, '%*s %s %*s');
This will create a space between my desired string.

sed search multiple strings and output each string and its following string to a separate line

For example;
I have a long file which contains:
Somestring anotherstring -xone xcont othertring -yone ycont againother \
-detail "detail Contents within quote" stuff morestuff ..
Somestring anotherstring -xone xcont othertring -yone ycont againother \
morestrings -detail detailCont morestrings etc.. ..
The desired out:
-xone xcont
-ycont ycont
-detail "detail Contents withing quote"
Would be ideal to have a csv file with:
xone yone detail
xcont ycont "detail Contents within quote"
What is the best approach to get the desired output? I have been trying with sed commands with very limited sucess. I am new to perl so didnt get far there either.. Please explain the suggested solution.
Thanks in Advance!
This problem consists of two parts:
How to match the tags
How to output them in an orderly fashion.
The matching part is quite simple, using a regex. Each tag is a hyphen-minus followed by some word characters. As a regex pattern: -\w+.
The value seems to either be a word (which we can match like \w+) or a quoted string. Assuming this string cannot contain its delimiter, we can use "[^"]+" where [^"] is a negated character class that matches anything but the double quote character.
How do we combine this? With an alternation, and named captures:
# I'll answer with Perl
my $regex = qr/-(?<key>\w+) \s+ (?: (?<val>\w+) | "(?<val>[^"]+)" )/x;
After that, $+{key} contains the key, and $+{val} the value for that tag. We can now extract all tags in a line. Given the input
Somestring anotherstring -xone xcont othertring -yone ycont againother \-detail "detail Contents within quote" stuff morestuff ..
Somestring anotherstring -xone xcont othertring -yone ycont againother \morestrings -detail detailCont morestrings etc.. ..
And the code
use strict; use warnings; use feature 'say';
my $regex = ...;
while (<>) {
while (/$regex/g) {
say qq($+{key}: "$+{val}");
}
}
we get the output
xone: "xcont"
yone: "ycont"
detail: "detail Contents within quote"
xone: "xcont"
yone: "ycont"
detail: "detailCont"
To print that out in a tabular format, we have to collect the data in a certain structure. I will assume that each tag can occur once for each line. Then we can use a hash to define the mapping from tags to their values. We collect these hashes in an array, one for each line. We must also collect the names of all headers, in case one line does not contain all headers. Now our code changes to:
use strict; use warnings; use feature 'say';
my $regex = ...;
my %headers;
my #rows;
while (<>) {
my %tags;
while (/$regex/g) {
$tags{$+{key}} = $+{val};
}
push #rows, \%tags;
#headers{keys %tags} = (); # define the headers
}
Now how do we print the data out? We could just dump them as tab separated values:
my #headers = keys %headers;
say join "\t", map qq("$_"), #headers;
say join "\t", map qq("$_"), #$_{#headers} for #rows;
Output:
"yone" "detail" "xone"
"ycont" "detail Contents within quote" "xcont"
"ycont" "detailCont" "xcont"
Oh, and the order of columns is random. We can do better if we use the Text::CSV module. Then:
use Text::CSV;
my #headers = keys %headers;
my $csv = Text::CSV->new({ eol => "\n" });
$csv->print(\*STDOUT, \#headers);
$csv->print(\*STDOUT, [#$_{#headers}]) for #rows;
And we get the output:
yone,xone,detail
ycont,xcont,"detail Contents within quote"
ycont,xcont,detailCont
The order of the columns is still random, but this could be fixed through sorting.
You can read through the Text::CSV documentation to discover many options how you could tweak the output.
This might work for you (GNU sed):
sed -r '/-(xone|yone|detail)/!d;s//\n\1/;s/[^\n]*\n//;s/\S+\s+("[^"]*"|\S+)/&\n/;P;D' file
This looks for lines containing the string -xone, -yone or -detail and prints only them and the following words enclosed by "'s or another word.

Replace numbers in a string

I Have some text file. theses texts contain a string like this(a part of text):
<abbr class="word p1"">dd</abbr>
<img src"D:\Images\1.png">
<abbr class="word p1">dd</abbr>
<img src"D:\ticket\t\1.png">
In each text file,(D:\Images\1.png) png name is different but it is always numbers(from 1 to 114)for example(1,2,3,10,...)
I want to replace this text D:\Images\[number].png with a specific text for expample:
string newtext=Replace("D:\Images\[number].png","Something");
How can i do this?
thanks.
Use a regular expression:
string newtext = Regex.Replace(text, #"(D:\\Images\\)\d+(.png)","$1Something$2");
It will replace the full match, including D:\Images\ and .png, so $1 and $2 puts back what's caught by the parentheses, so that Somthing only replaces the digits.
Use regular expressions that are represented mostly be the Regex class. See these links:
http://www.codeproject.com/Articles/93804/Using-Regular-Expressions-in-C-NET
http://msdn.microsoft.com/en-us/library/ms228595%28v=vs.80%29.aspx

Resources