I'm not very familiar with regular expressions.
I'm trying to create a regular expression that will match the text between the first group of two forwards slashes. It's easiest just to show an example.
Search Texts:
/index
/index/
/index/foo/
/index/foo/bar/
All of those should return just "index"
Another example:
Search Texts:
/page.php
/page.php/foo?bar=1
Should return just "page.php" for both of those
Thanks alot guys!
Try this one for javascript or php preg_match: ^\/([^\/]*)
The pattern matches only of if there is a slash at the beginning and then matches everything that is not a slash.
Related
I need to create a route that responses to any string starting with '#' character. Routes like following examples :
www.mywebsite.com/#john
www.mywebsite.com/#jack
www.mywebsite.com/#something
So I wrote:
Route::get('{something}','SomeController#someMethod')->where('something','/#^/');
But when I test it, I face 404 not found found page.
what is the correct regular expression for this?
Route::get('/{tag}', 'SomeController#someMethod')->where('tag', '^#.*');
This will also work:
Route::get('#{something}', 'SomeController#someMethod');
You can write this
Route::pattern('tag', '#[a-zA-Z]');
Route::get('{tag}', 'SomeController#someMethod');
This way you seperate the logic of the regex and the route and it will work as you want
Note the #^ pattern means # should be followed with the beginning of string, which is not possible, and the pattern never matches any string. The '^#' pattern asserts the position at the start of the string, and only there does it try to match #.
Also, the usual / regex delimiters should be removed from this pattern as they are treated as part of the pattern here.
So, in your case you may just swap the anchor and the # char:
Route::get('{something}','SomeController#someMethod')->where('something','^#');
I have a bunch of html files.
I am trying to find all anchor links whose href attribute does not end with slash
For example :-
Helllo
This should match
Helllo
This should not match
How do i go on about making the regular expression.
Building on hjpotter92's answer...
Find: href="(\S*?[^/])"
Replace: href="$1/"
In order to make sure you only collect href attribute values from <a> elements, and to make sure you only match the link itself, you can use the following regex:
<a\s[^<>]*href="\K[^"]*?(?<=[^\/])(?=")
Or
<a\s[^<>]*href="\K[^"]*?(?=(?<=[^\/])")
The following pattern would work:
href="\S*?[^/]"
You can try:
Find: href="(.*?[^/])"
Replace with: href="$1/"
I'm trying to use the IIS 7 URL Rewrite feature for the first time, and I'm having trouble getting my regular expression working. It seems like it should be simple enough. All I need to do is rewrite a URL like this:
http://localhost/myApplication/MySpecialFolder
To:
http://localhost/MySpecialFolder
Is this possible? I want the regular expression to ignore everything before "myApplication" in the original URL, so that I could use "http://localhost" OR "http://mysite", etc.
Here's what I've got so far:
^myApplication/MySpecialFolder$
But using the "Test Pattern..." feature in IIS, it says my patterns don't match unless I supply "myApplication/MySpecialFolder" exactly. Does anyone know how I can update my regular expression so that everything prior to "myApplication" is ignored and the following URLs will be seen as a match?
http://localhost/myApplication/MySpecialFolder
http://mysite/myApplication/MySpecialFolder
Many thanks in advance!
SOLUTION:
I needed to change my regex to:
myApplication/MySpecialFolder
Without the ^ at the beginning and without the $ at the end.
Your regular expression is correct, the pattern will be matched against path starting after the first slash after the domain.
So only bold part will be used for matching: http://localhost/myApplication/MySpecialFolder
To limit the rewriting to specific domain you have to use Conditions section with Condition input = {HTTP_HOST}
Unless there is something radically different with regexes in IIS, you would want to take out the anchor (^) at the beginning to match.
myApplication/MySpecialFolder$
The carat ^ tells it that that is the beginning of the string and the dollar sign $ tells it to match the end. A regex like abc finds "abc" anywhere in the string, ^abc matches strings that start with "abc", abc$ matches strings that end with "abc", and ^abc$ only matches when the whole string is "abc".
I am trying to match google urls from some text that is stored in a variable, using the pattern below.
The urls use double quotes
QRegExp regExp;
regExp.setPattern("http://www.google.com/(.*)");
I manage to match the url but it unwontedly matches all of the text that is contained after it. I have tried using similar variants like the ones below, but they don't seem to work.
regExp.setPattern("http://www.google.com/(.*)\"is");
regExp.setPattern("http://www.google.com/^(.*)$\"");
Any help to get a regular expression that matches just the url alone.
Thanks in advance
Is there a reason you need/want to use a QRegExp?
You could use a QUrl most likely.
Even though it is impossible for us to know what is around the urls in your text (quotes ? parenthesis ? white spaces ?), we can create a better regular expression by trying to do a negative match of characters that cannot be part of the url:
QRegExp regExp;
regExp.setPattern("http://www.google.com/([^()\"' ]*)");
Then you just need to add more possible characters to this negative character class.
I have to extract all there is between this caracters:
<a href="/url?q=(text to extract whatever it is)&
I tried this pattern, but it's not working for me:
/(?<=url\?q=).*?(?=&)/
I'm programming in Vb.net, this is the code, but I think that the problem is that the pattern is wrong:
Dim matches As MatchCollection
matches = regex.Matches(TextBox1.Text)
For Each Match As Match In matches
listbox1.items.add(Match.Value)
Next
Could you help me please?
Your regex is seemed to be correct except the slash(/) in the beginning and ending of expression, remove it:
Dim regex = New Regex("(?<=url\?q=).*?(?=&)")
and it should work.
Some utilities and most languages use / (forward slash) to start and end (de-limit or contain) the search expression others may use single quotes. With System.Text.RegularExpressions.Regex you don't need it.
This regex code below will extract all urls from your text (or any other):
(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,#?^=%&:/~\+#]*[\w\-\#?^=%&/~\+#])?