I'm using the function replace in a freemaker template like this:
example?replace("<|>|_|!", ' ', 'r')
The character '<' is not replaced. It can work with this ( I can't explain it=:
example?replace('<', ' ')
Any help ?
Thank you :)
Related
l = ['sone.com']
for a in l:
if '#' and '.' in a:
print('ok')
Result is coming to be ok. Why?
if '#' and '.' in a:
print('ok')
is causing the error, change it to
if '#' in a and '.' in a:
print('ok')
when you do if '#' on its own, it returns true.
Think of if '#' is the same as saying if '#' = '#'.
and when you use and the two variables need to be set individual condtions so ...
if '#' in a and '.' in a
notice the if '#' in a & '.' in a
I'm trying to find a regexp that catches all instances that contain at least one \n and any number of (space), no matter the order. So, for instance (with spaces denoted with _), all of these should be caught by the regexp:
\n
\n\n\n\n
\n\n\n_\n\n
_\n
\n_
_\n_
_\n\n
\n\n_
_\n\n_
_\n\n_\n
\n_\n_
_\n\n_\n_
___\n__\n and so on...
However, it must not catch spaces that do not border a \n.
In other words, I'd like to reduce all of this (if I'm not making any mistake) to one line:
import re
mystring = re.sub(r'(\n)+' , '\n' , mystring)
mystring = re.sub(r'( )+' , ' ' , mystring)
mystring = re.sub(r'\n ' , '\n' , mystring)
mystring = re.sub(r' \n' , '\n' , mystring)
mystring = re.sub(r'(\n)+' , '\n' , mystring)
mystring = re.sub(r'(\n)+' , ' | ' , mystring)
[ ]*(?:\n[ ]*)+
or, if you want to match tabulations:
[ \t]*(?:\n[ \t]*)+
Demo & explanation
You can use the following regular expression:
(( )*\n+( )*)+
I have a line of code that is saying that there is invalid syntax in my print statement. Does anyone know about how to fix this? I've tried deleting the parentheses and have tried changing the +'s to ,'s.
print(stockName[random] + ' - $' + Names[0, amod] + ' : You have ' + x + ' of this stock')
If you use ' x ' with + operator it should be a string else you should use coma.
I have this line and now wants to replace not only dots and underline by a space. Now I also would replace the word "German" (without the quotes) by a blank line.
Can anybody help ?
preg_replace('/\(.*?\)|\.|_/i', ' ',
best regs
Edit:
public function parseMovieName($releasename)
{
$cat = new Category;
if (!$cat->isMovieForeign($releasename))
{
preg_match('/^(?P<name>.*)[\.\-_\( ](?P<year>19\d{2}|20\d{2})/i', $releasename, $matches);
if (!isset($matches['year']))
preg_match('/^(?P<name>.*)[\.\-_ ](?:dvdrip|bdrip|brrip|bluray|hdtv|divx|xvid|proper|repack|real\.proper|sub\.?fix|sub\.?pack|ac3d|unrated|1080i|1080p|720p|810p)/i', $releasename, $matches);
if (isset($matches['name']))
{
$name = preg_replace('/\(.*?\)|\.|_/i', ' ', $matches['name']);
$year = (isset($matches['year'])) ? ' ('.$matches['year'].')' : '';
return trim($name).$year;
}
}
return false;
}
The string is for example "movieName German 2015" but the output should be "movieName 2015" (without the quotes)
Solved:
Change now the line preg_replace('/\(.*?\)|\.|_/i', ' ', $matches['name']); to $name = preg_replace('/\h*\bGerman\b|\([^()]*\)|[._]/', ' ', $matches['name']);
Thanks # Wiktor Stribiżew
To add an alternative to an alternation group, you just need to use
$name = preg_replace('/\h*\bGerman\b|\([^()]*\)|[._]/', ' ', $matches['name']);
^^^^^^ ^^^^^^^
Note that \h matches horizontal whitespace only (no linebreaks), if you need linebreaks, use \s.
The \h*\bGerman\b matches zero or more spaces followed by a whole word "German" (as \b is a word boundary, no "Germanic" word will be matched).
Also, (\.|_) is equal to [._] in the result this pattern matches, but a character class [...] is much more efficient when matching single symbols.
How to write a regular expression for a text field which accepts all characters except a comma (,) and do not accept a white space at both the ends? I have tried
[^,][\B ]
but no use
like 'product generic no' instead of 'product,generic,no' or ' product generic no '
I suggest a solution without regular expression. As you said you're using JS so the function is in JavaScript:
function isItInvalid(str) {
var last = str.length - 1;
return (last < 2 ||
str[0] == ' ' ||
str[last] == ' ' ||
str.indexOf(',') != -1);
}
EDIT: Just made it a bit more readable. It also checks if the string is at least 3 chars.
Something like below:
/^\S[^,]*\S$/
Using a Perl regular expression
/^\S[^,]*\S$/
This should work from 2 characters up, but fails in the edge case where the string has only one non-comma character. To cover that too:
/^((\S[^,]*\S)|([^\s,]))$/