Login Login
MORE

WIDGETS

Widgets

Wanted articles
Who is online?
Article tools

CSharp:CSharpRegularExpressionEsempiUso

From Aino Wiki

Jump to: navigation, search

Sulle stringhe

Pulizia

Come rimuovere numeri da stringhe alfanumeriche:

string strIn = "ciccio123";
string strOut = Regex.Replace(strIn, @"[\d-]", string.Empty);
//strOut --> 123

Selezione

Esempio realistico. Seguono i dati in forma grezza da cui si riempie la lista di oggetti: lstDraftInfo

BT1 order.Data	    IMO9876543/ShipData/TransducerMeasurements/Transducers/TH/BT1 order.Data	     0	14:32:37.739
BT3 order.Data	    IMO9876543/ShipData/TransducerMeasurements/Transducers/TH/BT3 order.Data	    -1	14:32:37.739
BT2 response.Data	IMO9876543/ShipData/TransducerMeasurements/Transducers/TH/BT2 response.Data	    -1	14:32:37.739
ST1 response.Data	IMO9876543/ShipData/TransducerMeasurements/Transducers/TH/ST1 response.Data	     0	14:32:37.739
ST2 response.Data	IMO9876543/ShipData/TransducerMeasurements/Transducers/TH/ST2 response.Data	     0	14:32:37.739
ST3 response.Data	IMO9876543/ShipData/TransducerMeasurements/Transducers/TH/ST3 response.Data	    -1	14:32:37.739

Ecco il codice:

DateTime sourceTimestampUTC = SqlDateTime.MinValue.Value;
Regex regexNodeDisplayName = new Regex(@"^(BT|ST)([0-9]+) (order|response)\.Data$");
 
try
{
	#region Acquisizione CICLICA dati STRINGA
	foreach (DraftInfoSubscrChanged draftInfo in lstDraftInfo)
	{
		nodeDisplayName = draftInfo.ID;
		//Get last SourceTimestampUTC
		if (sourceTimestampUTC < draftInfo.SourceTimestampUTC)
		{ 
			sourceTimestampUTC = draftInfo.SourceTimestampUTC;
		}
 
		if (regexNodeDisplayName.IsMatch(nodeDisplayName))
		{
			Match regExpMatchParts = regexNodeDisplayName.Match(nodeDisplayName); // Riferiti alle parentesi TONDE --^
			// Es: BT1 dove: values.Groups[1].Value = "BT", values.Groups[2].Value = "1"
			String id = String.Format("{0}{1}", regExpMatchParts.Groups[1].Value, regExpMatchParts.Groups[2].Value); 
 
			ThrusterInfo thruster = ShipData.Instance.Thrusters.Where(x => x.ID == id).SingleOrDefault();
			if (thruster == null)
			{
				thruster = new ThrusterInfo();
				thruster.ID = id; // BT1
				ShipData.Instance.Thrusters.Add(thruster);
			}
 
			// Assegnamento dei valori
			if (regExpMatchParts.Groups[3].Value == "response")
			{
				thruster.Act.SetValue(double.Parse(draftInfo.Value, CultureInfo.InvariantCulture), 
									m_sourceName, draftInfo.SourceTimestampUTC);
			}
			else // order
			{
				thruster.Ref.SetValue(double.Parse(draftInfo.Value, CultureInfo.InvariantCulture), 
									m_sourceName, draftInfo.SourceTimestampUTC);
			}
		}
		else
		{
			throw new Exception(string.Format("Thrusters attribute '{0}', unknow.", nodeDisplayName));
		}
	} // foreach (DraftInfoSubscrChanged draftInfo in lstDraftInfo)
	#endregion
}
catch (Exception ex)
{
	ok = false;
	m_logger.Error("NodeDisplayName: {0}'\r\nEx: {1}\r\n{2}", nodeDisplayName, ex.Message, ex.StackTrace);
}

Verifica

 


Mappa e Link


C# | Alias_PaginaPadre


Visual Studio


Parole chiave:Espressioni regolari, regular expression, regEx

Author