How to extract middle string of a path name in python? - os.path

Suppose i have a path:
p= 'MOC/TA/RC/AO/date/number/letter/all/output/filetype'
how can i get AO ?
I tried os.path.split(p), but it gets me only 'MOC/TA/RC/AO/date/number/letter/all/output' and 'filetype'.
what I want is 'AO' .

Related

kamailio: how to get a value from SIP INVITE Header

Please help me to get the a value from SIP INVITE header reached to kamailio like INVITE sip:+341930203454#sub.domain.com;myid=+34#sub.domain.com SIP/2.0 and i want to save the myid value +34 into a variable, without the domain name.
$var(uri) = $sel(ruri);
xavp_params_explode("$(var(uri){s.unbracket})", "uri");
xlog("L_INFO", "$var(uri) Received converted to $xavp(uri=>myid[0])\n");
I tried above and it prints +34#sub.domain.com But i want to just save +34 into a variable to further check the prefix based routing from the database.
Could you please help how to get it or If there is any alternate/single line approach to get this value, please help.
thanks in advance.
An one line statement variant can be:
$var(result) = $(ru{uri.param,myid}{s.select,0,#});
Where:
$ru - is the request URI (sip address in the first line of request)
{uri.param,myid} - is a transformation returning the value of a parameter (myid in this case) in a URI
{s.select,0,#} - is a transformation returning first token from a string by splitting it using the delimiter #
I think something like this will work:
$var(uri) = $sel(ruri);
$var(myid-param) = $(var(uri){param.value,myid}); # this should return +34#sub.domain.com
$var(sip-myid-param) = "sip:" + $var(myid-param); # this should return sip:+34#sub.domain.com
$var(user-part) = $(var(sip-myid-param){uri.user}); # this should return +34
All above string operation is possible to put in one line, I used several lines just to show it in simple way.
In one line it should be something like below, but please do not quote me on this, but I hope you can get an idea
$var(user-part) = $(var("sip:" + $var($(var(uri){param.value,myid}))){uri.user});

Getting an empty string when scanned

So I'm trying to create a Graph from a file input. The first line of the file contains an int and the second line in the file contains either a D or a U for directed and undirected. But when I did some troubleshooting to try and find the error in my code I found that my Scanner was scanning the second line as an empty string instead of the letter D. Here's my code. I rewrote the file so I know the file isn't wrong.
The file is:
7
D
(0, 1)
(0, 3)
.
.
.
Code:
public static Graph createFromFile(String filename) throws FileNotFoundException
{
File file = new File(filename);
Scanner scan = new Scanner(file);
int line1 = scan.nextInt();
vertexCount = line1;
System.out.println(vertexCount);
String line2 = scan.nextLine();
System.out.println(line2);
String linne2 = "\"" + line2 + "\"";
System.out.println(linne2);
if(linne2.equalsIgnoreCase("D")){directed = true;}
else if(linne2.equalsIgnoreCase("U")){directed = false;}
else{System.out.println("This is not the proper input");}
}
The output is:
7
""
This is not the proper input
Does anybody know where this issue is coming from?
scan.nextInt function doesn't take the execution to next line. So, scan.nextLine call is still on your first line and it returns rest of the data from first line , which in this case is basically space. Use one more scan.nextLine and it will give you expected result.
Also , please go through the documentation of Scanner class , it will clear your doubts

Regex to get parts of URL

Hi I have URL as follows:
vimeo.com/99612902
www.vimeo.com/99612902
http://vimeo.com/99612902
http://www.vimeo.com/99612902
http://vimeo.com/moogaloop.swf?clip_id=81368903
I need to parse the above URL to get two group as folloes:
Group1 Group 2
vimeo.com/ 99612902
www.vimeo.com/ 99612902
http://vimeo.com/ 99612902
http://www.vimeo.com/ 99612902
http://vimeo.com/ 81368903
I've tried the followin regex
^((http[s]?|ftp):\/)?\/?([^:\/\s]+)(:([^\/]*))?((\/[\w\-]+)*\/)([\w\-\.]+[^#?\s]+)(\?([^#]*))?(#(.*))?
but which yields me unwanted and empty group. Please help me out.
With your input, we can match both parts into Groups 1 and 2 with this:
^(.*/)(.*)
or, for your revised input:
^(.*[/=])([^/=]+$)
In the demo, see the capture groups in the right pane.
In VB.NET, you can do this:
Dim theUrl As String
Dim theNumbers As String
Try
ResultString = Regex.Match(SubjectString, "^(.*/)(.*)", RegexOptions.Multiline)
theUrl = ResultString.Groups(1).Value
theNumbers = ResultString.Groups(2).Value
Catch ex As ArgumentException
'Syntax error in the regular expression
End Try
Option 2
If you want to do some very lightweight url validation at the same time, you can use this:
^((?:http://)?(?:www\.)?[^./]+\.\w+/)(.*)
or, with your revised input:
^((?:http://)?(?:www\.)?[^./]+\.\w+[=/])([^/=]+$)
If you don't want to validate the url then try this as well. Get the matched group from index 1 and 2.
(.*?[^\/]*\/)(\d+)
Here is DEMO
String literals for use in programs: C#
#"(.*?[^\/]*\/)(\d+)"
Simply you could use the below regex,
^(.*\/)(.*)$
DEMO
From the starting upto the last / symbol are captured by group1. Remaining characters are captured into group2.
OR
^((?:https?:\/\/)?(?:www\.)?(?:[^.]*)\.\w+\/)(.*)$
DEMO

How can i replace url from css

I'm trying to combine multiple css files into a single file (from many different folder in my host, and including external css files). I use the following short code:
Declare array:
$styles=array(
array(
'name'=>'style1.css',
'path'=>'http://mydomain.com/css/'
),
array(
'name'=>'camera.css',
'path'=>'http://mydomain.com/js/camera/'
),
array(
'name'=>'style3.css',
'path'=>'http://external.com/assets/css/'
));
Get content and replace url:
foreach ($styles as $style) {
echo preg_replace('/url\(\s*[\'"]?\/?(.+?)[\'"]?\s*\)/i', 'url('.$style['path'].'$1)', file_get_contents($style['path'].$style['name']));
}
After combined into one css file, i have some css background image url as follows:
url(ttp://mydomain.com/css/../image/background.png) //Internal path - Case 1A
url(http://mydomain.com/js/camera/../../image/background.png) //Internal path - Case 1B
url(http://external.com/assets/css/../../image/background.png) //External path - Case 2
Actual, the internal path (Case 1A, 1B) can display the background image (despite the lack of professionalism), but in the external path (Case 2) cannot display the background image, my question is:
How can i replace wrong path with correct path (REPAIR BASE ON CURRENT RESULTS) as:
url(http://mydomain.com/image/background.png) //Correct internal path
url(http://external.com/image/background.png) //Correct external path
(I understand the problem is if find a keyword containing '../' will remove keyword and remove 1 string before the keyword contain '*/', , but I can't figure out how to do this).
Thanks in advance.
(I have tried find out and tried searching for 1 week before ask new question).
additionally replace /up-level-directory/../ with /
that is '/([^\/]+)/../' with / globally
$correct_url = preg_replace('|/\w+/\.\./|', '/', $url);

xQuery substring problem

I now have a full path for a file as a string like:
"/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/TRN_282C_HYD_MOD_1_Drive_Shaft_Rev000.xml"
However, now I need to take out only the folder path, so it will be the above string without the last back slash content like:
"/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/"
But it seems that the substring() function in xQuery only has substring(string,start,len) or substring(string,start), I am trying to figure out a way to specify the last occurence of the backslash, but no luck.
Could experts help? Thanks!
Try out the tokenize() function (for splitting a string into its component parts) and then re-assembling it, using everything but the last part.
let $full-path := "/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/TRN_282C_HYD_MOD_1_Drive_Shaft_Rev000.xml",
$segments := tokenize($full-path,"/")[position() ne last()]
return
concat(string-join($segments,'/'),'/')
For more details on these functions, check out their reference pages:
fn:tokenize()
fn:string-join()
fn:replace can do the job with a regular expression:
replace("/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/TRN_282C_HYD_MOD_1_Drive_Shaft_Rev000.xml",
"[^/]+$",
"")
This can be done even with a single XPath 2.0 (subset of XQuery) expression:
substring($fullPath,
1,
string-length($fullPath) - string-length(tokenize($fullPath, '/')[last()])
)
where $fullPath should be substituted with the actual string, such as:
"/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/TRN_282C_HYD_MOD_1_Drive_Shaft_Rev000.xml"
The following code tokenizes, removes the last token, replaces it with an empty string, and joins back.
string-join(
(
tokenize(
"/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/TRN_282C_HYD_MOD_1_Drive_Shaft_Rev000.xml",
"/"
)[position() ne last()],
""
),
"/"
)
It seems to return the desired result on try.zorba-xquery.com. Does this help?

Resources