Regex for "anything between" 2 patterns [duplicate] - node.js

This question already has answers here:
Regular expression to get a string between two strings in Javascript
(13 answers)
Closed 5 years ago.
I have a string with multiple URLs and other content in it.
I have URLs in following format:
http://www.example.com/bla1/bla2/thumb_my-file-name(2).JPG
The part till http://www.example.com/bla1/bla2/thumb_ is fixed.
However, I dont know what will be: my-file-name(2). I know it will be ending with .JPG
So far, I'm using the following regex to detect them:
/http:\/\/www.example.com\/bla1\/bla2\/thumb_/g
and I have also used \.JPG to detect the end.
But how do I capture the middle part as well?
How do I catch this whole URL?

Try this regex pattern:
http://www.example.com/bla1/bla2/thumb_(.*?)\.JPG
Code:
var url = "http://www.example.com/bla1/bla2/thumb_my-file-name(2).JPG";
var myRegexp = /http:\/\/www.example.com\/bla1\/bla2\/thumb_(.*?)\.JPG/g;
var match = myRegexp.exec(url);
console.log(match[1]); // my-file-name(2)

Related

Parse URL from a gibberish string (Nodejs) [duplicate]

This question already has answers here:
how to detect and get url on string javascript [duplicate]
(2 answers)
Closed 1 year ago.
I have been looking for a way to parse URL from a string.
My current goal is to parse
https://example.com/foo.png
from a string like
abcxyz https://example.com/foo.png gibberfish text.
Anyone got a solution or a package that can help me to do the job?
Thanks in advance.
text = "abcxyz https://example.com/foo.png gibberfish text."
console.log(text.split(" ")[1])
.split() splits the string at spaces into an array of words.
You can refer: https://www.w3schools.com/jsref/jsref_split.asp
I recommend this solution. It turns all words into an array and picks out the ones starting with https://
text.split(" ").filter(i => i.startsWith("https://")).toString();

Python re.sub to capture groups unexpected behaviour [duplicate]

This question already has answers here:
Regular expression to stop at first match
(9 answers)
Python non-greedy regexes
(7 answers)
Closed 2 years ago.
Consider following code:
import re
mystring = "\enquote {aaa} a \enquote {bbb}"
text = re.sub(r"\\enquote \{(.+)\}", r"\1", mystring)
print(text)
is outputing:
"aaa} a \enquote {bbb"
but I am acrually trying to achieve output of:
"aaa a bbb"
What have I misunderstood?
Background: I am trying to do some simple conversion from LaTeX format to general text, for which I need to replace LaTeX commands, but retain raw text itself (and sometimes do also some actual replacing). So how can I do group capture in python?

Remove filename.extension in url using nodejs

I'm a beginner in nodejs, so please excuse if this question already answered. I tried multiple methods but didn't work for me.
I'm trying to remove the filename.extension in http url
For example:
http://somedomain.com/path1/path2/path3/myfile.txt
to
http://somedomain.com/path1/path2/path3/
The filename in the url is dynamic, so I cannot use "myfile.txt" explicitly in the code.
I'm not using any web frameworks, But I have http://stringjs.com library
Using a Regular Expression:
'http://somedomain.com/hello1/hello2/hello3/myfile.txt'.replace(/\/\w+\.\w+$/, '');
The regular expression matches two strings separated by a . and preceeded by a / (/myfile.txt in your case), which is then being replaced by an empty string. This method works in node as well as in pure javascript.
Using node.js' path module:
let path = require('path');
let parsed = path.parse('http://somedomain.com/hello1/hello2/hello3/myfile.txt');
console.log(parsed.dir) // => http://somedomain.com/hello1/hello2/hello3
node.js has a built-in module for parsing paths. It is however not made for parsing URIs, but should work just fine in your case.
Splitting, Slicing and Joining
let url = 'http://somedomain.com/hello1/hello2/hello3/myfile.txt';
url.split('/').slice(0, -1).join('/');
Split the url at every /, remove the last element from the resulting array (myfile.txt) and join them back together with / as the separator.
You can do it like this:
var path = 'http://somedomain.com/hello1/hello2/hello3/myfile.txt';
path = path.split('/');
path.splice(path.length-1,1);
path = path.join('/');

Split string in Lua, save in 2 variables [duplicate]

This question already has answers here:
Split string in Lua?
(18 answers)
Closed 9 years ago.
I have one string, that is = s = "Pedro Martinez, Defense"
I want to split the string before the comma and after the comma, store those cuts on 2 variables, for example:
I think that I need to use the string.gmatch function or string.sub
How can I do that?
The accepted answer at How to convert GPS coordinates to decimal in Lua? shows how to use string.match and patterns. Applying the same techniques as mentioned there, you could use
local name, expertise = string.match(s, "(.*),%s*(%a*)")
which would lead to name being "Pedro Martinez" and expertise being "Defense".

PHP Strict Standards on line with variable [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Annoying PHP error: “Strict Standards: Only variables should be passed by reference in”
I have this line of code,
$extension=end(explode(".", $srcName));
when I fun my function I get
PHP Strict Standards: Only variables should be passed by reference in
I am not sure how to solve this
The function end() requires a variable to be passed-by-reference and passing the return-value of a function doesn't acheive this. You'll need to use two lines to accomplish this:
$exploded = explode(".", $srcName);
$extension = end($exploded);
If you're simply trying to get a file-extension, you could also use substr() and strrpos() to do it in one line:
$extension = substr($srcName, strrpos($srcName, '.'));
Or, if you know the number of .'s that appear in the string, say it's only 1, you can use list() (but this won't work if there is a dynamic number of .'s:
list(,$extension) = explode('.', $srcName);

Resources