I need help to built regular expression for
string which does not start with pcm_ or PCM_
any guess!!!
No need to use regular expression. Use String.startsWith() method.
if (!str.StartsWith("pcm_",StringComparison.InvariantCultureIgnoreCase)) {}
if (String.startsWith("pcm_") || String.startsWith("PCM_"))
{
//...
}
The regex solution would be
^(?i)(?!pcm_)
(?i) is the inline version of RegexOptions.IgnoreCase
^ matches the start of the string
(?!pcm_) is a negative lookahead assertion, that is true if the string does not start with "pcm_" or "PCM_" (but also "PcM_, ...)
As already pointed out, you don't need to use regular expressions for this, but if you wanted to you could use one with negative lookahead like so: ^(?!pcm_|PCM_).*$
see similar link
Regex pattern for checking if a string starts with a certain substring?
No need for a Regex here, simply use String.StartsWith http://msdn.microsoft.com/en-us/library/system.string.startswith.aspx
Related
How do I put a regular expression to check if a string starts with certain pattern and is NOT ending with certain pattern.
Example:
Must StartsWith: "US.INR.USD.CONV"
Should not end with: ".VALUE"
Passes Regex: "US.INR.USD.CONV.ABC.DEF.FACTOR"
Fails Regex Check: "US.INR.USD.CONV.ABC.DEF.VALUE"
I am using C#.
You can use this regex based on negative lookahead:
^US\.INR\.USD\.CONV(?!.*?\.VALUE$).*$
RegEx Demo
Explanation:
^US\.INR\.USD\.CONV - Match US.INR.USD.CONV at start of input
(?!.*?\.VALUE$) - Negative lookahead to make sure line is not ending with .value
^US\.INR\.USD\.CONV.*(?<!\.VALUE)$
Try this.See demo.
https://regex101.com/r/fA6wE2/26
Just use a negative lookbehind to make .VALUE is not before $ or end of string.
(?<!\.VALUE)$ ==>Makes sure regex engine looks behind and checks if `.VALUE` is not there when it reaches the end of string.
You don't need regular expressions for that. You can just use String.StartsWith and String.EndsWith
if(val.StartsWith("US.INR.USD.CONV") && !val.EndsWith(".VALUE"))
{
// valid
}
And as you mention in your comment to anubhava's answer you can do this to check for ".PERCENT" at the end as well.
if(val.StartsWith("US.INR.USD.CONV") &&
!val.EndsWith(".VALUE") &&
!val.EndsWith(".PERCENT"))
{
// valid
}
IMHO this makes the code much more readable and will almost definitely perform faster as well.
I want a Regular expression for a string which can have 4 to 6 digits number and a comma to separate them which is already done by this regular expression:
#"^(\d{1,6},{1})+$"
What i don't want is comma at the end
This existing regular expression works for string of type:0234,23544,234332,
I want a regular expression for following string:0234,23544,234332.
I am still unclear about the (.) at the end or not
but /^\d{1,6},+\d+$/ will match any string as asked not ending with , or .
Demo and Regex Explained
This ^\d{1,6}(,\d{1,6})*$ should do the job. If you want 4-6 digits in a group, replace {1,6} with {4,6}.
i would advice
(\d{4,6}[,|\.])
if you match 'global' e.g. all occurrences you will get this:
0234,
23544,
234332.
0234,
23544,
234332,
take a look here!
for perl it would be
m/(\d{4,6}[,|\.])/g
try this regular expression
^\d{1,6}(,\d{1,6})*(\.)?$
Simplest is probably:
/^(\d{4,6},?\b)+$/
How to write regular expression that matches only numbers,text and only these two special characters # and -
[a-zA-Z0-9#-\s]+ anything else should be rejected
You could a regular expression similar to this
^(\d|[a-z]|[A-Z]|#|-)*$
Below the code I used for checking the regular expression in c#:
string input = #"343243-2df---ds#SFD#FD";
string pattern = #"^(\d|[a-z]|[A-Z]|#|-)*$";
Console.WriteLine(Regex.Match(input, pattern).Success);
Console.ReadLine();
Also, for future reference material, you could see this regular expression site: http://www.regular-expressions.info/
I believe the following pattern would work in C#:
string pattern = #"^[a-zA-Z\d\s#-]*$";
I just tried a couple of test cases in C# interactive:
Regex.Match("", pattern).Success
> true
Regex.Match("asdf 2F#-#-", pattern).Success
> true
Regex.Match("asdf~asdf", pattern).Success
> false
If you don't want to accept an empty string, simply change the * to + in the pattern. Also, you didn't state that you wanted to match whitespace in your question but your example pattern does match whitespace. If you don't want to match whitespace, remove the \s.
Update: Edited per Rawling's suggestions.
Try this:
^([a-z]|[A-Z]|[#]|[0-9]|[-])+$
I want to replace
! = change
# = static(does not)
$ = Want to replace
I got a string like this #!$!
How do I replace the $ with something else?
EDIT: I need to use Regex as the string may appear anywhere!
You don't need a regular expression, just use the String.Replace method:
String result = input.Replace("$", "somethingElse");
As a side note: The way that you would do this with a regular expression would be like this:
String result = Regex.Replace(input, #"\$", "somethingElse");
Notice that I have escaped the $ with a backslash since $ usually means match the end of the string.
Take a look at System.Text.RegularExpressions.Regex.Replace method.
Regex.Replace("#!$!", "!(.*)!", "replacement value");
Why do you want some RegExp for string replacement. You can just use string.Replace() fundtion.
also, check out Rubular, a great RegEx Tester.
Using the String class' .Replace() method would do the trick but, if you really want to use RegEx, this is a great RegEx site that I use quite often.
Regular Expression Library
You should be able to find what you're looking for there.
Is the best way to do this with Regex? I don't want it picking up partial words for example if I'm search for Gav it shouldn't match Gavin.
Any examples would be great as my regular expression skills are non existant.
Thanks
Yes, a Regex is perfect for the job.
Something like:
string regexPattern = string.Format(#"\b{0}\b", Regex.Escape(yourWord));
if (Regex.IsMatch(yourString, regexPattern)) {
// word found
}
What you want is probably like this:
if (Regex.IsMatch(myString, #"\bGav\b")) { ... }
The \b:s in the regex indicate word boundaries, i.e. a whitespace or start/end of the string. You may also want to throw in RegexOptions.IgnoreCase as the third parameter if you want that. Note that the #-sign in front of the regex is essential, otherwise it gets misinterpreted due to the double meaning of the \ sign.