How to remove \/ slashes I want only / in Drupal8 JSON Output - drupal-8

[{"field_backgroundimage":"http:\/\/localhost\/drupal\/sites\/default\/files\/2016-10\/image1_3.jpg"}]
How to remove \/ slashes, I want only /. I think it is coming in Raw JSON Format. How to Convert It into Pretty JSon Format. In Postman it is coming fine in Pretty format.

can you not post-treat the json output ?
$string = str_replace("\/", "/", $string);

Related

Mongo DB escape front slash

If collection(Area) has template like below
{
'path': '/city/area/street/house'
}
then how do we use like query here(how do i escape front slash)
db.getCollection('Area').find({ "path":/.city/area/street/house./})
this does not work
/.city\/area\/street\/house./
\ will escape the / inside regex.
It worked using Regex like below and we don't need to escape slashes
db.getCollection('Area').find({"path":{'$regex':'city/area/street/house'}})
This does not work
db.getCollection('Area').find({ "path":/.city\/area\/street\/house./})

MicroPython Regex not matching although it does online

I have a strange Problem. When I parse my Regex online it works fine, but in MicroPython doesn't match it.
regex:
()*<div>(.*?)<\/div>()*or<div>(.*?)<\/div>or<div>(.*?)</div>
toMatch:
<Storage {}>86400<div>Uhrzeit in Sekunden: 65567</div><div>Timer: 20833</div>
none of these match with python but do online (http://regexr.com/ or https://pythex.org/)
This is just a short part of what i want to get. But what i want is the data inside the div.
EDIT:
I am using micropython on a esp8266. I am limited and cant use a html parser.
I suspect your problem is that you are not passing a raw string to re.compile(). If I do this I get what I think you want:
>>> rx = re.compile(r"<div>(.*?)<\/div>")
>>> rx.findall("<Storage {}>86400<div>Uhrzeit in Sekunden: 65567</div><div>Timer: 20833</div>")
>>> ['Uhrzeit in Sekunden: 65567', 'Timer: 20833']
You need a raw string because \ is both the Python string escape character and the regex escape character. Without it you have to put \\ in your regex when you mean \ and that very quickly becomes confusing.

Regexp with slashes and path to files

Want to parse some logs, a bit hard.
Logs look like this:
/ajax/foto.php?whatever-session-info-here
/edit.php?path=blahblah-imgage-url.jpg
/catalog/whetaever-text-here
/item/whetaever-text-here
/gallery (without slash at the end)
So
/[a-zA-Z-]{0,}/
works good for text between slashes, and i have
/catalog/
/item/
after regexp work
So question is how to get output for this example that looked like:
/ajax/foto.php
/edit.php
/catalog/
/item/
/gallery
ADD:
found this, need only text betwen first two slashes:
/foto/300/b/5/4/19123312.jpg
to get /foto/
/[a-zA-Z](/?)([a-zA-Z].[a-zA-Z]*)?
Tested on
http://gskinner.com/RegExr/
or
s = '/foto/300/b/5/4/19123312.jpg'
s.split('/')[1]
=> "foto"

Regex to get a filename from a url

I am trying to write a regex to get the filename from a url if it exists.
This is what I have so far:
(?:[^/][\d\w\.]+)+$
So from the url http://www.foo.com/bar/baz/filename.jpg, I should match filename.jpg
Unfortunately, I match anything after the last /.
How can I tighten it up so it only grabs it if it looks like a filename?
The examples above fails to get file name "file-1.name.zip" from this URL:
"http://sub.domain.com/sub/sub/handler?file=data/file-1.name.zip&v=1"
So I created my REGEX version:
[^/\\&\?]+\.\w{3,4}(?=([\?&].*$|$))
Explanation:
[^/\\&\?]+ # file name - group of chars without URL delimiters
\.\w{3,4} # file extension - 3 or 4 word chars
(?=([\?&].*$|$)) # positive lookahead to ensure that file name is at the end of string or there is some QueryString parameters, that needs to be ignored
This one works well for me.
(\w+)(\.\w+)+(?!.*(\w+)(\.\w+)+)
(?:.+\/)(.+)
Select all up to the last forward slash (/), capture everything after this forward slash. Use subpattern $1.
Non Pcre
(?:[^/][\d\w\.]+)$(?<=\.\w{3,4})
Pcre
(?:[^/][\d\w\.]+)$(?<=(?:.jpg)|(?:.pdf)|(?:.gif)|(?:.jpeg)|(more_extension))
Demo
Since you test using regexpal.com that is based on javascript(doesnt support lookbehind), try this instead
(?=\w+\.\w{3,4}$).+
I'm using this:
(?<=\/)[^\/\?#]+(?=[^\/]*$)
Explanation:
(?<=): positive look behind, asserting that a string has this expression, but not matching it.
(?<=/): positive look behind for the literal forward slash "/", meaning I'm looking for an expression which is preceded, but does not match a forward slash.
[^/\?#]+: one or more characters which are not either "/", "?" or "#", stripping search params and hash.
(?=[^/]*$): positive look ahead for anything not matching a slash, then matching the line ending. This is to ensure that the last forward slash segment is selected.
Example usage:
const urlFileNameRegEx = /(?<=\/)[^\/\?#]+(?=[^\/]*$)/;
const testCases = [
"https://developer.mozilla.org/en-US/docs/Web/API/MutationObserverInit#yo",
"https://developer.mozilla.org/static/fonts/locales/ZillaSlab-Regular.subset.bbc33fb47cf6.woff2",
"https://developer.mozilla.org/static/build/styles/locale-en-US.520ecdcaef8c.css?is-nice=true"
];
testCases.forEach(testStr => console.log(`The file of ${testStr} is ${urlFileNameRegEx.exec(testStr)[0]}`))
It might work as well:
(\w+\.)+\w+$
You know what your delimiters look like, so you don't need a regex. Just split the string. Since you didn't mention a language, here's an implementation in Perl:
use strict;
use warnings;
my $url = "http://www.foo.com/bar/baz/filename.jpg";
my #url_parts = split/\//,$url;
my $filename = $url_parts[-1];
if(index($filename,".") > 0 )
{
print "It appears as though we have a filename of $filename.\n";
}
else
{
print "It seems as though the end of the URL ($filename) is not a filename.\n";
}
Of course, if you need to worry about specific filename extensions (png,jpg,html,etc), then adjust appropriately.
> echo "http://www.foo.com/bar/baz/filename.jpg" | sed 's/.*\/\([^\/]*\..*\)$/\1/g'
filename.jpg
Assuming that you will be using javascript:
var fn=window.location.href.match(/([^/])+/g);
fn = fn[fn.length-1]; // get the last element of the array
alert(fn.substring(0,fn.indexOf('.')));//alerts the filename
Here is the code you may use:
\/([\w.][\w.-]*)(?<!\/\.)(?<!\/\.\.)(?:\?.*)?$
names "." and ".." are not considered as normal.
you can play with this regexp here https://regex101.com/r/QaAK06/1/:
In case you are using the JavaScript URL object, you can use the pathname combined with the following RegExp:
.*\/(.[^(\/)]+)
Benefit:
It matches anything at the end of the path, but excludes a possible trailing slash (as long as there aren't two trailing slashes)!
Try this one instead:
(?:[^/]*+)$(?<=\..*)
This is worked for me, no matter if you have '.' or without '.' it take the sufix of url
\/(\w+)[\.|\w]+$

URL safe characters RegEx that will allow UTF-8 accents!

I'm looking for a RegEx pattern to use in a rereplace() function that will keep URL safe characters, but include UTF-8 characters with accents. For example: ç and ã.
Something like: url = rereplace(local.url, "pattern") etc. I prefer a ColdFusion only solution, but I'm open to using Java too since it's so easy to integrate with CF.
My URL pattern will look like: /posts/[postId]/[title-with-accents-like-ç-and-ã]
I don't know what language you are using. Perl has some utf8 matching, see for example Tatsuhiko Miyagawa's URI::Find::UTF8
This can be done by matching alpha numeric characters using \w.
rereplace(string, "[^\w]", "", "all")
See this answer for reference.