I have a string containg alphabetic characters, for example:
254.69 meters
26.56 cm
23.36 inches
100.85 ft
I want to remove all the alphabetic characters (units) from the above mentioned strings so that I can call the double.Parse() method.
This should work:
// add directive at the top
using System.Text.RegularExpressions;
string numberOnly = Regex.Replace(s, "[^0-9.]", "")
You should be able to solve this using Regex. Add the following reference to your project:
using System.Text.RegularExpressions;
after that you can use the following:
string value = Regex.Replace(<yourString>, "[A-Za-z ]", "");
double parsedValue = double.Parse(value);
Assuming you have only alphabetic characters and space as units.
Using LINQ:
using System.Linq;
string input ="57.20000 KG ";
string output = new string(input.Where(c=>(Char.IsDigit(c)||c=='.'||c==',')).ToArray());
Hi another solution is:
// add directive at the top
using System.Text.RegularExpressions;
string s = "24,99";
string numberOnly = Regex.Replace(s, "[^0-9,-]+", "")
This solution will not remove the dot e.g. question from user1804084:
this remove the dot for me e.g. 24.99 somechracter -> 2499
However it can still be convertet to a double where adding and subtraction works as normal.
The Regex, and Vlad's LINQ answers cover the solution well. And are both good options.
I had a similar problem, but I also wanted to only explicitly strip letters, and not to strip white space or etc, with this variant.
I also wanted it usable as below. Any of the other solutions could be packaged in a similar manner.
public static string StripAlpha(this string self)
{
return new string( self.Where(c => !Char.IsLetter(c)).ToArray() );
}
public static string StripNonNumeric(this string self)
{
// Use Vlad's LINQ or the Regex Example
return new string(self.Where(c=>(Char.IsDigit(c)||c=='.'||c==',')).ToArray()) ; // See Vlad's
}
This would then be used such as:
var newString = someString.StripAlpha();
var newString2 = someString.StripNonNumeric();
Use CharMatcher API from Google's Guava library:
String magnitudeWithUnit = "254.69 meter";
String magnitude = CharMatcher.inRange('a', 'z').or(inRange('A', 'Z')).removeFrom(magnitudeWithUnit);
Do static-import CharMatcher.inRange(..). You can trim the results for trailing space.
Related
I have following string
string s=#"\Users\Public\Roaming\Intel\Wireless\Settings";
I want output string like
string output="Wireless";
Sub-string what I want should be after "Intel\" and it should ends with the first "\" after "Intel\" before string Intel and after Intel the string may be different.
I have achieved it using string.substring() but I want to get it using regular expression ? what regular expression should I write to get that string.
For a regex solution you may use:
(?<=intel\\)([^\\]+?)[\\$]
Demo
Notice the i flag.
BTW, Split is much simpler and faster solution than regexes. Regex is associated with patterns of string. For a static/fixed string structure, it is a wise solution to manipulate it with string functions.
With regex, it will look like
var txt = #"\Users\Public\Roaming\Intel\Wireless\Settings";
var res = Regex.Match(txt, #"Intel\\([^\\]+)", RegexOptions.IgnoreCase).Groups[1].Value;
But usually, you should use string methods with such requirements. Here is a demo code (without error checking):
var strt = txt.IndexOf("Intel\\") + 6; // 6 is the length of "Intel\"
var end = txt.IndexOf("\\", strt + 1); // Look for the next "\"
var res2 = txt.Substring(strt, end - strt); // Get the substring
See IDEONE demo
You could also use this if you want everything AFTER the intel/
/(?:intel\\)((\w+\\?)+)/gi
http://regexr.com/3blqm
You would need the $1outcome. Note that $1 will be empty or none existent if the string does not contain Intel/ or anything after it.
Why not use Path.GetDirectoryName and Path.GetFileName for this:
string s = #"\Users\Public\Roaming\Intel\Wireless\Settings";
string output = Path.GetFileName(Path.GetDirectoryName(s));
Debug.Assert(output == "Wireless");
It is possible to iterate over directory components until you find the word Intel and return the next component.
I have a label that contains up to 255 characters. In that label users can write words including words with hashtags such as :"This is an image at the hotel in #Thailand". Now I want #Thailand to be displayed as a link. Is there a simple and straight forward way to check the words that start with hashtags? Here is a sample of what I would like to accomplish.
Label photoCaptionLabel = FindControl("photoCaptionLabel") as Label;
string photoCaptionLabelText = photoCaptionLabel.Text;
string[] split = photoCaptionLabelText.Split();
foreach (string s in split)
{
// if the word starts with hashtag turn it into a link }
...
You can use StartsWith method
if(s.StartsWith("#"))
Alternatively you can also check for the first char:
if(s[0] == '#')
This is prone to error is s is an empty string.You can use RemoveEmptyEntries option in your Split method to avoid this.
Here I wrote function to do it, just pass photo caption label text to it:
private static string HashTag(string photoCaptionLabelText)
{
//Regular expression to find hash tag with word after hash w+ and put it to 2 grouping
//1 grouping will be hash #
string replacePattern = #"(#)(\w+)";
//replacement string will replace #Thailand matched by our pattern with htPage.aspx?ht=#Thailand
//$2 is back reference that will carry word after hash tag to your link
//you can make it a function to generate your custom links and where you will put $2 you will get word from hash tag
string replacementString = #"htPage.aspx?ht=#$2";
//Actual replacement
return Regex.Replace(inputText, replacePattern, replacementString);
}
You can split with regex, so it would work no matter what whitespace characters (and how many of them) user entered, and you could use LINQ .Where method to make it nicer, cleaner and in one line ;)
using System;
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;
namespace ConsoleApplication1 {
class Program {
static void Main(string[] args) {
var text = "test1 test2 test3 #tag1 #tag2 #tag3";
var tags = Regex.Split(text, #"\s+").Where(i => i.StartsWith("#"));
Debug.Print("TAGS: " + String.Join(", ", tags));
}
}
}
How do I check if a string contains the following characters "RM" followed by a any number or special character(-, _ etc) and then followed by "T"?
Ex: thisIsaString ABRM21TC = yes, contains "RM" followed by a number and followed by "T"
Ex: thisIsaNotherString RM-T = yes, contain "RM" followed by a special character then followed by "T"
Your going to want to check the string using a regex (regular expression). See this MSDN for info on how to do that
http://msdn.microsoft.com/en-us/library/ms228595.aspx
Try this regexp.
[^RM]*RM[^RMT]+T[^RMT]*
Here is a sample program.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication12
{
class Program
{
static void Main(string[] args)
{
String rg = "[^RM]*RM[^RMT]+T[^RMT]*";
string input = "111RM----T222";
Match match = Regex.Match(input, rg, RegexOptions.IgnoreCase);
Console.WriteLine(match.Success);
}
}
}
You can do it with a simple regular expression:
var match = Regex.Match(s, "RM([^T]+)T");
Check if the pattern is present by calling match.Success.
Get the captured value by calling match.Groups[1].
Here is a demo (on ideone: link):
foreach (var s in new[] {"ABRM21TC", "RM-T", "RxM-T", "ABR21TC"} ) {
var match = Regex.Match(s, "RM([^T]+)T");
Console.WriteLine("'{0}' - {1} (Captures '{2}')", s, match.Success, match.Groups[1]);
}
It prints
'ABRM21TC' - True (Captures '21')
'RM-T' - True (Captures '-')
'RxM-T' - False (Captures '')
'ABR21TC' - False (Captures '')
Use regular expressions
http://www.webresourcesdepot.com/learn-test-regular-expressions-with-the-regulator/
The Regulator is an advanced, free regular expressions testing and learning tool. It allows you to build and verify a regular expression against any text input, file or web, and displays matching, splitting or replacement results within an easy to understand, hierarchical tree.
You should play around with more sample data especially regarding special characters, you can use regexpal, I have added the two cases and an expression to get you started.
I want to strip off everything but numbers, $, comma(,).
this only strip letters
string Cadena;
Cadena = tbpatronpos6.Text;
Cadena = Regex.Replace(Cadena, "([^0-9]|\\$|,)", "");
tbpatronpos6.Text = Cadena;
Why doesn't my regex work, and how can I fix it?
I suspect this is what you want:
using System;
using System.Text.RegularExpressions;
class Test
{
static void Main(string[] args)
{
string original = #"abc%^&123$\|a,sd";
string replaced = Regex.Replace(original, #"[^0-9$,]", "");
Console.WriteLine(replaced); // Prints 123$,
}
}
The problem was your use of the alternation operator, basically - you just want the set negation for all of (digits, comma, dollar).
Note that you don't need to escape the dollar within a character group.
you want something like this?
[^\\d\\$,]
I have a string:
productDescription
In it are some custom tags such as:
[MM][/MM]
For example the string might read:
This product is [MM]1000[/MM] long
Using a regular expression how can I find those MM tags, take the content of them and replace everything with another string? So for example the output should be:
This product is 10 cm long
I think you'll need to pass a delegate to the regex for that.
Regex theRegex = new Regex(#"\[MM\](\d+)\[/MM\]");
text = theRegex.Replace(text, delegate(Match thisMatch)
{
int mmLength = Convert.ToInt32(thisMatch.Groups[1].Value);
int cmLength = mmLength / 10;
return cmLength.ToString() + "cm";
});
Using RegexDesigner.NET:
using System.Text.RegularExpressions;
// Regex Replace code for C#
void ReplaceRegex()
{
// Regex search and replace
RegexOptions options = RegexOptions.None;
Regex regex = new Regex(#"\[MM\](?<value>.*)\[\/MM\]", options);
string input = #"[MM]1000[/MM]";
string replacement = #"10 cm";
string result = regex.Replace(input, replacement);
// TODO: Do something with result
System.Windows.Forms.MessageBox.Show(result, "Replace");
}
Or if you want the orginal text back in the replacement:
Regex regex = new Regex(#"\[MM\](?<theText>.*)\[\/MM\]", options);
string replacement = #"${theText} cm";
A regex like this
\[(\w+)\](\d+)\[\/\w+\]
will find and collect the units (like MM) and the values (like 1000). That would at least allow you to use the pairs of parts intelligently to do the conversion. You could then put the replacement string together, and do a straightforward string replacement, because you know the exact string you're replacing.
I don't think you can do a simple RegEx.Replace, because you don't know the replacement string at the point you do the search.
Regex rex = new Regex(#"\[MM\]([0-9]+)\[\/MM\]");
string s = "This product is [MM]1000[/MM] long";
MatchCollection mc = rex.Matches(s);
Will match only integers.
mc[n].Groups[1].Value;
will then give the numeric part of nth match.