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, #"\.\.", ".");
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have been trying to make a regex expression which can match all the below strings. I was able to make a regex for first two, but unable to do for the next 2. The regex should be such that the matching is performed only until the end of error message.
RAISERROR 20001 #errmsg;
RAISERROR 20001 #errmsg
RAISERROR 20001 #'ajhsgdjh jahsgdjahsgdjg'
RAISERROR 20001 #'ajhsgdjh jahsgdjahsgdjg';
Below is the test link:
https://regex101.com/r/ntywvP/3
((?i)raiserror)\s*\d{5,6}\s*([^;|\s*]*)
Any help is appreciated. Thank you in advance.
You could replace ([^;|\s*]*) by (#[^;\r\n]*). You could try the following regex.
(?i)raiserror\s*\d{5,6}\s*(#[^;\r\n]*)
Demo
I would use this:
\bRAISERROR \d+ #(?:'[\w ]+'|[\w ]+);?
Demo
I deal with the optional single quoted message by using an alternation which allows either for this, or for a message sans single quotes.
My suggestion is https://regex101.com/r/ntywvP/4
^((?i)raiserror)\s*\d{5,6}\s*(.+?)(;|\s*)$
Capturing the string beginning with # is done via matching a s few characters as possible with (.+?). This allows specifying possible characters after the wildcard before the end of the line.
^ matches line start
$ matches line end
You can match all between the single quotes or match only non whitespace chars. If you don't need the capturing group around ((?i)raiserror) you can omit it.
(?i)raiserror\s*\d{5,6}\s*#(?:'[^'\r\n]*'|\S+);?
Explanation
(?i) case insensitive modifier
raiserror\s*\d{5,6}\s* Match raiserror and 5-6 digits between optional whitespace chars
# Match the # char
(?: Non capture group for the alternation
'[^'\r\n]*' Match any char except ' between an opening and closing '
| Or
\S+ Match 1+ non whitespace chars
);? Close group and match optional ;
Regex demo
Based on your question you can use either:
'.'
This matches everything in the world with at least one character, so it will match all four of your strings.
Or to match the four strings exactly:
'RAISERROR 20001 #errmsg|RAISERROR 20001 #errmsg|RAISERROR 20001 #'ajhsgdjh jahsgdjahsgdjg|RAISERROR 20001 #'ajhsgdjh jahsgdjahsgdjg'
You haven't specified the rules for what strings you want to match and what strings you don't. Any other solutions require guessing on what you mean but haven't explained.
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I want to make sure a first name field has at least one alphanumeric character and also allow spaces and dashes.
**VALID**
David
Billie Joe
Han-So
**INVALID**
-
Empty is also invalid
To ensure the dashes and spaces happen in legitimate places, use this:
(?i)^[a-z]+(?:[ -]?[a-z]+)*$
See demo.
(?i) puts us in case-insensitive mode
^ ensures we're at the beginning of the string
[a-z]+ matches one or more letters
[ -]?[a-z]+ matches an optional single space or dash followed by letters...
(?:[ -]?[a-z]+)* and this is allowed zero or more times
$ asserts that we have reached the end of the string
You mentioned alphanumeric, so in case you also want to allow digits:
(?i)^[a-z0-9]+(?:[ -]?[a-z0-9]+)*$
use this pattern
^(?=.*[a-zA-Z])[a-zA-Z -]+$
Demo
oh, for alphanumeric use
^(?=.*[a-zA-Z0-9])[a-zA-Z 0-9-]+$
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.
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 11 years ago.
I need to extract the value 33345.002 from the following string:
"ABC(MAX)(33345.002)"
How can I perform this in C#?
I tried handling it in SQL but was picking up the (MAX) too so now I'm gonna try C#.
Thanks
.
.
.
This is the closest so far :
string temp = "YYY(33345.002)(gg)YYYY";
temp = Regex.Replace(temp, "[^.0-9]", "");
double num;
bool success = Double.TryParse(temp, out num);
if (success)
{
//do what ever to the number}
but there is a problem, some of the numbers have zeros in front of them. like: 00033.33
This is really pretty simple.
Declare the characters you want to grap [0-9]/"0123456789" as a constant in C#
loop through the string, example:
public bool TryParseDouble(string input, out double value){
if(string.IsNullorWhiteSpace(input)) return false;
const string Numbers = "0123456789.";
var numberBuilder = new StringBuilder();
foreach(char c in input) {
if(Numbers.IndexOf(c) > -1)
numberBuilder.Append(c);
}
return double.TryParse(numberBuilder.ToString(), out value);
}
Ofcoarse this could be enhanced (perhaps just parse out the first number, or return an array of doubles parsing out all numbers) - not to mention it will parse out multiple decimals which is not exactly what you want.
The same technique can be used in T-SQL as well with looping over the string, declaring the valid values then using 'in'.
EDIT: On second thought
Regex.Match(input, #"\d+(.\d+)?")
would extract double/decimal from string then you could just use double.Parse if a match is found :).
EDIT 2: Btw for some silly reason '\ .' gets escaped as '.' on Stack Overflow. Just note that the decimal in the regex is escaped (just . matches anything)
Happy coding!
You need the same regex (including the enhancements mentioned) as provided as the top answer by J-16 SDiZ for this SO: Regular expression for decimal number, but without the ^ at the beginning and $ at the end.
Next time it might be worth searching a bit on SO or Google first :)