What Regex do I need for match this url:
Match:
1234
1234/
1234/article-name
Don't match:
1234absd
1234absd/article-name
1234/article.aspx
1234/any.dot.in.the.url
You can try:
^\d+(?:\/[\w-]*)?$
This matches a non-empty sequence of digits at the beginning of the string, followed by an optional suffix of a / and a (possibly empty) sequence of word characters (letters, digits, underscore) and a -.
This matches (see on rubular):
1234
1234/
1234/article-name
42/section_13
But not:
1234absd
1234absd/article-name
1234/article.aspx
1234/any.dot.in.the.url
007/james/bond
No parenthesis regex
You shouldn't need to do this, but if you can't use parenthesis at all, you can always expand to alternation:
^\d+$|^\d+\/$|^\d+\/[\w-]*$
^\d+(/?)|(/[a-zA-Z-]+)$
That may work. or not. Hope it helps
Hope this ll help u ........
string data = "1234/article-name";
Regex Constant = new Regex("(?<NUMBERS>([0-9]+))?(//)?(?<DATA>([a-zA-Z-]*))?");
MatchCollection mc;
mc = Constant.Matches(data,0);
if (mc.Count>0)
{
for (int l_nIndex = 0; l_nIndex < mc.Count; l_nIndex++)
{
string l_strNum = mc[l_nIndex].Groups["NUMBERS"].Value;
string l_strData = mc[l_nIndex].Groups["DATA"].Value;
}
}
Related
I have been trying to get a regex expression to return me the following in the following situations.
XX -> XX
XXX -> XXX
XX/XX -> XX
XX/XX/XX -> XX/XX
XXX/XXX/XX -> XXX/XXX
I had the following Regex, however they do no work.
^[^/]+ => https://regex101.com/r/xvCbNB/1
=========
([A-Z])\w+ => https://regex101.com/r/xvCbNB/2
They are close but are not there.
Any Help would be appreciated.
You want to get all text from the start till the last occurrence of a specific character or till the end of string if the character is missing.
Use
^(?:.*(?=\/)|.+)
See the regex demo and the regex graph:
Details
^ - start of string
(?:.*(?=\/)|.+) - a non-capturing group that matches either of the two alternatives, and if the first one matches first the second won't be tried:
.*(?=\/) - any 0+ chars other than line break chars, as many as possible upt to but excluding /
| - or
.+ - any 1+ chars other than line break chars, as many as possible.
It will be easier to use a replace here to match / followed by non-slash characters before end of line:
Search regex:
/[^/]*$
Replacement String:
""
Updated RegEx Demo 1
If you're looking for a regex match then use this regex:
^(.*?)(?:/[^/]*)?$
Updated RegEx Demo 2
Any special reason it has to be a regular expression? How about just splitting the string at the slashes, remove the last item and rejoin:
function removeItemAfterLastSlash(string) {
const list = string.split(/\//);
if (list.length == 1) [
return string;
}
list.pop();
return list.join("/");
}
Or look for the last slash an remove it:
function removeItemAfterLastSlash(string) {
const index = string.lastIndexOf("/");
if (index === -1) {
return string;
}
return string.splice(0, index);
}
I have a quantifier regular expression that matches a 5digit code [0-9]{5}.
How can I exclude any matched of the above quantifier?
I tried [^([0-9]{5})] but it seems it doesn't work.
Test data follows:
including:
12345678875645 (will be matched)
pppppaaaaa (will be matched)
52p26 (will be matched)
123 (will be matched)
excluding:
12345 (won't be matched)
try this
^(\d{1,4}|\d{6,})$
This won't match numbers with exactly 5 digits
demo here: https://regex101.com/r/sHvRMA/1
You can use a negative look ahead:
/(?!^[0-9]{5}$)^.+$/
var rexp = /(?!^[0-9]{5}$)^.+$/;
var str = ['12345', '12345678875645', 'pppppaaaaa', '52p26', '123'];
for (var i = 0; i < str.length; i++) {
console.log(str[i] + ' - ' + (rexp.test(str[i]) ? 'matched' : 'did not match'));
}
I assume that you need a regex to match all things except 5 digits length
You simply need to use negative lookahead assertion for excluding 5 digits. that is it.
\b(?!\d{5}).+|.{6,}\b
It excludes only 5 digits not anything else
I have the following string
$FileNamePattern = 'blah_{4}_{5}_blah_{4}-{2}.CSV'
and I want to replace the numbers in the curly braces with a string of question marks, n characters long
As an example I would like it to return 'blah_????_?????_blah_????-??.CSV'
I have this so far, but can't seem to get the 'expansion' in the replace working
[regex]::Replace($FileNamePattern,'{(\d+)}','"?"*$1')
Any help would be greatly appreciated!
Matthew
You need to do the processing of the match inside a callback method:
$callback = { param($match) "?" * [int]$match.Groups[1].Value }
$FileNamePattern = 'blah_{4}_{5}_blah_{4}-{2}.CSV'
$rex = [regex]'{(\d+)}'
$rex.Replace($FileNamePattern, $callback)
The regex {(\d+)} matches { and } and captures 1+ digits in between. The submatch is parsed as an integer inside the callback (see [int]$match.Groups[1].Value) and then the ? is repeated that amount of times with "?" * [int]$match.Groups[1].Value.
I want to place a dash after every letter but my regex place a dash at the end too. How can I improve my regex?
String outputS = dnaString.replaceAll("(.{1})", "$1-");
(.)(?!$)
You can use this.Replace by $1.See demo.
https://regex101.com/r/gT6vU5/11
(?!$) uses negative lookahead to state that do not capture a character which is at end of string.
Without regex (that is faster):
String[] nucleotides = dnaString.split("");
String outputS;
int seqLength = nucleotides.length;
if (seqLength > 1) {
StringBuilder sb = new StringBuilder();
sb.append(nucleotides[0]);
for (int i = 1; i < seqLength; i++) {
sb.append("-");
sb.append(nucleotides[i]);
}
outputS = sb.toString();
} else {
outputS = dnaString;
}
I know this is an old question, but for completeness and future reference I would like to add this answer.
In Java 8 you can also use:
String.join("-",dnaString.toCharArray());
Explanation:
String.join(delimiter,objects...);
String.join(delimiter,array);
String.join(delimiter,Iterable);
These are used to join all objects to a single string with the delimiter as separator.
dnaString.toCharArray();
This is a method to get a String as an char array.
This replaces all special characters with underscore '_' except the last occurence of a special character in the string.
String name = "one-of-the dummy$ string:i.txt"; // input
name = name.replaceAll("[^a-zA-Z0-9](?=.*[^a-zA-Z0-9])", "_");
System.out.println(name);
//input: one-of-the dummy$ string:i.txt
//output: one_of_the_dummy__string_i.txt
This
(.)\B
doesn't match the last char.
See https://regex101.com/r/p0Z0zA/1
So, in your case, should be:
String outputS = dnaString.replaceAll("(.{1})\\B", "$1-");
Credits to pigreco.
I have a string something like "[aaa][vad][adf]", i would like to use regex to capture the data in[], and chars in [] can be char and number and no length limit. I am regex noob, can anyone help me on this?
Thanks.
You can try something like this:
var data = "[asd][dfhg][asfsa243]";
var re = new Regex(#"\[([^\]]*)\]");
var matches = re.Matches(data);
for (int i = 0; i < matches.Count; i++ )
{
var m = matches[i];
Console.WriteLine(m.Groups[1]);
}
Console.ReadLine();
This outputs:
asd
dfhg
asfsa243
The regular expression \[([^\]]*)\] matches zero or more occurrences of a character that isn't the ] character and which is between a pair of square brackets ([ and ])
This regex might work for you:
\[(\w+)\]
That if you by char refers to word character
Here is my suggestion:
\[(\w+)\]
The charachter will be provided in the first group of the match, like this:
var regex = new Regex(#"\[([\w\d]*)\]");
MatchCollection matchCollection = regex.Matches(stringToTest);
foreach (Match match in matchCollection)
{
Debug.Print(match.Groups[0].Value);
}
Here are some good resources for building regex's
http://www.regexlib.com
http://regexpal.com/
Here is the regex for Alphanumeric:
^[a-zA-Z0-9]+$
Here's how to ensure there are 3:
/^([a-zA-Z0-9]){3}$/
You could try -
[^\[\]\W]+
var matches = System.Text.RegularExpressions.Regex.Matches("[aaa][vad][adf]",#"[^\[\]\W]+").Cast<Match>().Select(m => m.Value);
That should ignore any '[', ']' and non-word characters and would return 'aaa', 'vad' and 'adf' from your example string.