This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I need a regex for this format xxxx-xxx-xx.jpg where x is digits [0-9].
To match for example: 3402-560-27.jpg
This regex is what you want:
\d{4}-\d{3}-\d{2}\.jpg
\d represents digits so \d{4} mean 4 digits.. in regex a . matches any single character so to match a literal . it needs to be escaped with a \.
This is one of the simplest regexes to write: put \d for each digit, - for each dash, and a \. for each dot. Letters correspond to themselves, so jpg goes in unchanged.
When you have more time, you can earn some "points for style" by learning about the explicit quantifier notation for repeated groups.
Related
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I want to check whether my string variable contain the particular regular expression pattern or not
xxx-xx-x
(x is numerical value) using c#. If it contains then I need to return true or false.
Can anyone please help me to resolve this issue..
Use the returned value by Regex.IsMatch().
Regex regex = new Regex("[0-9]{3}-[0-9]{2}-[0-9]");
bool containsPattern = regex.IsMatch(stringToVerify);
This is the regex you are looking for
\b\d{3}-\d{2}-\d\b
\b is a boundary..If you don't use it,you would also match 111-22-345 or 111-22-3-33 which i guess you don't want to match
using System.Text.RegularExpressions;
public static bool ControlRegex(string input)
{
Match match = Regex.Match(input, #"([A-Za-z0-9\-]+)",RegexOptions.IgnoreCase);
if (match.Success)
{
return true;
}
}
You can try something like this... You have to put correct regular expression to secend parametre of Regex.Match...
You can find the correct regex with a regex program. For example "RegEx TestBed" you can download it from here; http://regextestbed.codeplex.com/releases/view/60833 with this program, you put your text in text area, and in pattern area you try to find correct regex. And below, in the the list area, and program shows you the matches according your regex, so you can try and find your correct regex...
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I want to retrieve a number from a string wherever the number starts with 8,9 or 6 and length of the number should be 8 OR 9 Characters. E.g 92000000,9200 0000,9200-0000.
How about this: (?<!\d)([896]\d{3})(?:[-\s]?)(\d{4})(?!\d).
The (?:[-\s]?) eats the optional delimiters space or dash as a non-capturing group.
You get your number by concatenating the match groups 1 and 2:
var input = new string[] {
"81000000", "92000000", "9200 0000", "9200-0000"
};
var regex = new Regex (#"(?<!\d)([896]\d{3})(?:[-\s]?)(\d{4})(?!\d)");
foreach (var str in input) {
var match = regex.Match (str);
Console.WriteLine ("TEST: {0} {1} - {2}", str, match.Success,
match.Groups [1].Value + match.Groups [2].Value);
}
I have also tried (?<!\d)([896]\d{3}(?:[-\s]?)\d{4})(?!\d) and that won't remove the delimiter character from the match result.
Try the below regex:
(?<!\d)[896]\d{3}([- ]?)\d{4}(?!\d)
Try to solve using Regexpal from next time.
Cheers.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Single regx for all these Conditions
1.Should allow only aphanumeric
2. along with only one space between words
3. Should allow only special characters like -.,'
4. Should not allow leading space, trailing space and consecutive blank space.
Valid input:
"testing with 2 regx solution"
Invalid input:
" testing with 2 regx solution" or "testing %^with 2 regx solution "
Try this
^(\w+\s)*\w+$
^ Start of string
( Start of group
\w+ Word of one or more characters
\s White space
) End of group
* Zero or more of the preeceding group
\w+ Word of one or more characters
$ End of string
inputString= Regex.Replace(inputString.Trim(),#"\s+"," ");
--SJ
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
What I have is a program that takes user's input from a textbox and adds a period to the end of it. The problem I'm trying to solve is if the user puts a period at the end of what they have typed then I want to be able to remove that period. I tried to use the string replace method but that only lets you do it for a single character. The next thing I thought about was regular expressions.
I tried this:
finalString = Regex.Replace(finalString, "..", ".");
but all it did was replace every character with a period. Is there a regular expression that would let me replace 2 periods that are next to each other?
. has a special meaning so you need to escape it with \
finalString = Regex.Replace(finalString, "\\.\\.$", ".");
or simply use verbatim symbol
finalString = Regex.Replace(finalString, #"\.\.$", ".");
adding $ at the end of the regex asserts if the position of the period is on the last part of the string.
If you want to use any of these characters as a literal in a regex, you need to escape them with a backslash.
the opening square bracket [, the backslash \, the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, the plus sign +, the opening round bracket ( and the closing round bracket ).
Good Read
. in regular expression means match any character..In your case .. means match any two characters and replace it with .
You should escape it like this \.
It should be
finalString = Regex.Replace(finalString, #"\.\.", ".");
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Suppose the string is:
string item = "t-ewrwerwerwerwer\r-rr\wrjkwlr";
I want to Replace all - except when it is preceded by r.
So resut will be
string cleanItem = "tewrwerwerwerwer\r-rr\wrjkwlr"'
What regular expression can be used?
I think this regular expression is a little more efficient:
-(?<!r-)
Or if your language doesn’t support negative look-behind assertions, use this expression:
(^|[^r])-
and replace it by \1 (first matching group).
A replacement on (?<!r)- by an empty string should do the trick I think.
(?<!r)-
As long as your regex flavor supports zero-width look-behind, that is.