For example I've got string /wiki/File:test.JPG I should check if it has one of extention "jpeg","jpg","png","gif"
Currently I've written that link.search(/.[j,p,g][p,i,n][e,g,f][g]?/gi) and it is works, but I'd like better regular expression.
Just write the regex as a list:
/\.(jpe?g|png|gif)$/gi
(note the escaped dot (.))
EDIT: Added a $.
Just for future reference this tool is invaluable when dealing with regex in flash:
Web Version:
http://gskinner.com/RegExr/
Desktop Version:
http://gskinner.com/RegExr/desktop/
Maybe something like this?
/\.(jpe?g|jpg|png|gif)$/gi
The dot must be escaped (with a slash) and I wrote $ to point to the fact it needs to be the end of the string.
var pattern: RegExp = /\.(jpe?g|png|gif)$/gim
If you also want case-insensitive matching try this:
\.(?i)(jpe?g|png|gif)$
The dot must be escaped, else it'd match any character :)
Related
I'm intrested in extracting c8127c6ea6a44c109b5e35ce61cd4b0096a9c6dc from a string that looks like this:
?t=c8127c6ea6a44c109b5e35ce61cd4b0096a9c6dc'
Here is my attempt at capturing the result in to a group.
?t=([a-e]\d+)'
Could anyone point me in the right direction, since this obviously isn't working?
http://regexr.com?383s6
You wanna put the \d in the [a-e] block and escape the ?:
\?t=([a-f\d]+)'
(and I assume you're looking for hexadecimal so it should be a-f?)
You can use this regex:
\?t=([a-e0-9]+)'
OR usig negation:
\?t=([^']+)'
In Ruby language you can try Rubular tool online to test your regular expressions.
\?t=(\w+)'
should do the match.
I need to find a regular expression which will support the following format
.[A-Z-a-z]/-
Would ^(\.[A-Za-z]\/-?)+$ work fine ?
i.e
.V/-.E/-
BUT THE - IS OPTIONAL
I trield ^(\.[A-Za-z]\/-)$
but i cannot seem to find a regular expression to support the - at the end
Could someone show me where i am going wrong please.
Thanks
If you want the regex to match the string ".V/-.E/-", you could use this:
^(\.[A-Za-z]\/\-?)+$
This should be as simple as adding a ? after the -. The whole regex would be ^\.[A-Za-z]\/-?$ (don't need the parens unless you are using backreferences)
I have a string with multiline as below.
rawMessage=sysUpTimeInstance-->0:0:00:05.00
snmpTrapOID.0-->linkDown.0.0
In the drools when portion i have written the condition as below.
rawMessage matches "(?i).*linkDown(.|\n|\r)*"
but it is not working.Please provide me some pointers to handle multiline.
Its not clear to me what you want to do/achieve. Your regex looks not wrong (I don't know the drools flavour and what you want to match).
In general (.|\n|\r)* is able to match any character including newlines. In your example there is no newline after "linkDown", so what should it match there?
Maybe you need to double escape (I don't know for drools) like this: (.|\\n|\\r)*.
Another possibility is to use the singleline modifer s (Again, I don't know if drools supports this modifier). This makes the . match also newline characters, could then look something like this
rawMessage matches "(?i)(?s).*linkDown.*"
or if it should only match multiline from "linkdown" on
rawMessage matches "(?i).*linkDown(?s).*"
Drools uses standard java regular expressions. As the previous answer mention, your expression looks wrong. And yes, you need to double escape special chars like you would do in java. Just check the javadoc for the Pattern class in the java API.
Here's the input string:
loadMedia('mediacontainer1', 'http://www.something.com/videos/JohnsAwesomeVideo.flv', 'http://www.something.com/videos/JohnsAwesomeCaption.xml', '/videos/video-splash-image.gif)
With this RegExp: \'.+.xml\'
... we get this:
'mediacontainer1', 'http://www.something.com/videos/JohnsAwesomeVideo.flv', 'http://www.something.com/videos/JohnsAwesomeCaption.xml'
... but I want to extract only this:
http://www.something.com/videos/JohnsAwesomeCaption.xml
Any suggestions? I'm sure this problem has been asked before, but it's difficult to search for. I'll be happy to Accept a solution.
Thanks!
If you want to get everything within quotes that starts with http:
(?<=')http:[^']+(?=')
If you only want those ending with .xml
(?<=')http:[^']+\.xml(?=')
It doesn't select the quotation marks (as you asked)
It's fast!
Fair warning: it only works if the regex engine you're using can handle lookbehind
Knowing the language would be helpful. Basically, you are having a problem because the + quantifier is greedy, meaning it will match the largest part of the string that it can. you need to use a non-greedy quantifier, which will match as little as possible.
We will need to know the language you're in to know what the syntax for the non-greedy quantifier should be.
Here is a perl recipe. Just as a sidenote, instead of .+, you probably want to match [^.]+.xml.
\'.+?.xml\'
should work if your language supports perl-like regexes.
This should work (tested in javascript, but pretty sure it would work in most cases)
'[^']+?\.xml'
it looks for these rules
starts with '
is followed by anything but '
ends in .xml'
you can demo it at http://RegExr.com?2tp6q
in .net this regex works for me:
\'[\w:/.]+\.xml\'
breaking it down:
a ' character
followed by a word character or ':' or '/' or '.' any number of times (which matches the url bit)
followed by '.xml' (which differentiates the sought string from the other urls which it will match without this)
followed by another ' character
I tested it here
Edit
I missed that you don't want the quotes in the result, in which case as has been pointed out you need to use look behind and look ahead to include the quotes in the search, but not in the answer. again in .net:
(?<=')[\w:/.]+\.xml(?=')
but I think the best solution is a combination of those offered already:
(?<=')[^']+\.xml(?=')
which seems the simplest to read, at least to me.
so, i have some kind of intern urls: for example "/img/pic/Image1.jpg" or "/pic/Image1.jpg" or just "Image1.jpg", and i need to match this "Image1.jpg" in other words i want to match last character sequence after / or if there are no / than just character sequence. Thank you in advance!
.*/(.*) won't work if there are no /s.
([^/]*)$ should work whether there are or aren't.
Actually you don't need regexp for this.
s="this/is/a/test"
s.substr(s.lastIndexOf("/")+1)
=> test
and it also works fine for strings without any / because then lastIndexOf returns -1.
s="hest"
s.substr(s.lastIndexOf("/")+1)
=> hest
.*/([^/]*)
The capturing group matches the last sequence after /.
The following expression would do the trick:
/([\w\d._-]*)$
Or even easier (but i think this has also been posted below before me)
([^/]+)$
A simple regex that I have tested:
\w+(.)\w+$
Here is a good site you can test it on: http://rubular.com/
In Ruby You would write
([^\/]*)$
Regexps in Ruby are quite universal and You can test them live here: http://rubular.com/
By the way: maybe there is other solution that not involves regexps? E.g File.basenam(path) (Ruby again)
Edit: profjim has posted it earlier.
I noticed you said in your comments you're using javascript. You don't actually need a regex for this and I always think it's nice to have an alternative to using regex.
var str = "/pic/Image1.jpg";
str.split("/").pop();
// example:
alert("/pic/Image1.jpg".split("/").pop()); // alerts "Image1.jpg"
alert("Image2.jpg".split("/").pop()); // alerts "Image2.jpg"
Something like .*/(.*)$ (details depend on whether we're talking about Perl, or some other dialect of regular expressions)
First .* matches everything (including slashes). Then there's one slash, then there's .* that matches everything from that slash to the end (that is $).
The * operates greedily from left to right, which means that when you have multiple slashes, the first .* will match all but the last one.