I need a regular expression that will only match to the String if it ends with the target that I am looking for. I need to locate a file with a specific extension, problem is this extension also comes in other files. For example I have two files named
B82177_2014-07-08T141507758Z.ccf
and
B82177_2014-07-08T141507758Z.ccf.done
I only want to grab the first of these and my pattern is:
.*\.ccf
but this grabs both.
Any suggestions appreciated, I am a newbie to regular expressions.
Use an end anchor ($):
.*\.ccf$
This will match any string that ends with .ccf, or in multi-line mode, any line that ends with .ccf.
$ is used to match the end of the string.
and can be used like
"string"$
like
xyz$
if you want to end with xzy
VanilaJS function base example
function isValidUrl(x){
let expression = /[-a-zA-Z0-9#:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()#:%_\+.~#?&//=]*)?/gi;
let regex = new RegExp(expression);
return x.match(regex) ? true : false
}
console.log( isValidUrl('www.domain.com') ) // true
console.log( isValidUrl('nothing') ) // false
Related
I am trying to write a reg expression to find match of strings / code in a database.
here is some of the sample code / string which i need to remove using the regular expression.
[b:1wkvatkt]
[/b:1wkvatkt]
[b:3qo0q63v]
[/b:3qo0q63v]
[b:2r2hso9d]
[/b:2r2hso9d]
Anything that match [b:********] and [/b:********]
Anybody please help me out. Thanks in advance.
You can use the following pattern (as stated by LukStorms in the comments):
\[\/?b:[a-z0-9]+\]
If you want to replace [b:********] with <b> (and also the closing one), you can use the following snippet (here in JavaScript, other languages are similar):
var regex = /\[(\/)?b:[a-z0-9]+\]/g;
var testText = "There was once a guy called [b:12a345]Peter[/b:12a345]. He was very old.";
var result = testText.replace(regex, "<$1b>");
console.log(result);
It matches an optional / and puts it into the first group ($1). This group can then be used in the replacement string. If the slash is not found, it won't be added, but if it is found, it will be added to <b>.
We are currently matching "service_hub*queue"
I want to ignore the case "service_hub_scout_dead_queue" and yet still match everything else.
What is the regular expression for that ?
This javascript sollution gives an array with the matches
var myText = 'service_hub_anything_queue Add service_hub_scout_dead_queue something service_hub_someting_queue else';
var myMatches = myText.match(/service_hub(?!_scout_dead_)\w+queue/g);
If you are rather interested in what follows a match
var mySplit = ('dummy'+myText).split(/service_hub(?!_scout_dead_)\w+queue/g).filter(function(txt,i) {return (i>0);})
I put 'dummy' and then filter away the first part to make it work both if the sting starts with a valid tag and when it does not.
Using negative lookbehind: "service_hub_.*?(?<!_scout_dead)_queue"
This appears to be widely supported by popular regex engines; I've tested with Java (or Scala, rather) just to make sure it works.
I need to validate a regular expression aganist a string veryt strictly only what is there in string. But at present i don't see such a function for regular expression since bot exec and test doesn't check strictly. for example say i have regular expression for validating 43a^2b^2, but even its 43a it's returning true, as a part of string is present in reg exp. i need true only when exact match is found, is there any way achieve this?
The code I am using at present:
var isRegExp:Boolean = regExp.test(value1);
Thanks in advance...
Using the beginning of string ^ and end of string $ tests should work.
var regExp:RegExp = /^43a$/;
regExp.test('43a'); //true
regExp.test('43a^2b^2'); //false
You can read more about anchors here.
I am not really good at writing regular expressions but i need to match some parts of a URL.
http://example.com/service-page/service1/sub-service2/other-service/
my attempt looks like
service-page/(.?.+?)$
and the capture looks like service1/sub-service2/other-service/
but i need only to match if there is constant service-page inside the url and to capture the last part of the url other-service
http://example.com/[service-page]/service1/sub-service2/[other-service]/
I have to confess that I don't have WP to test with, but based on working with other regex engines, I don't see why this shouldn't work:
service-page/.*?([^/]+)/?$
To capture the name of the last subfolder:
ServicePageSubFolder("http://example.com/service-page/service1/sub-service2/other-service/");
public string ServicePageSubFolder(string url)
{
Regex regex = new Regex(#"service-page.*/([^/]+)/?");
Match match = regex.Match(url);
if (match.Success)
return match.Groups[1].Value;
return null;
}
Good luck with your quest.
I am using vbscript regex to find self-defined tags within a file.
"\[\$[\s,\S]*\$\]"
Unfortunately, I am doing something wrong, so it will grab all of the text between two different tags. I know this is caused by not excluding "$]" between the pre and post tag, but I can't seem to find the right way to fix this. For example:
[$String1$]
useless text
[$String2$]
returns
[$String1$]
useless text
[$String2$]
as one match.
I want to get
[$String1$]
[$String2$]
as two different matches.
Any help is appreciated.
Wade
The RegEx is greedy and will try to match as much as it can in one go.
For this kind of matching where you have a specific format, instead of matching everything until the closing tag, try matching NOT CLOSING TAG until closing tag. This will prevent the match from jumping to the end.
"\[\$[^\$]*\$\]"
Make the * quantifier lazy by adding a ?:
"\[\$[\s\S]*?\$\]"
should work.
Or restrict what you allow to be matches between your delimiters:
"\[\$.*\$\]"
will work as long as there is only one [$String$] section per line, and sections never span multiple lines;
"\[\$(?:(?!\$\])[\s\S])*\$\]"
checks before matching each character after a [$ that no $] follows there.
No need to use regex. try this. If your tags are always defined by [$...$]
Set objFS = CreateObject( "Scripting.FileSystemObject" )
strFile=WScript.Arguments(0)
Set objFile = objFS.OpenTextFile(strFile)
strContent = objFile.ReadAll
strContent = Split(strContent,"$]")
For i=LBound(strContent) To UBound(strContent)
m = InStr( strContent(i) , "[$" )
If m > 0 Then
WScript.Echo Mid(strContent(i),m) & "$]"
End If
Next