|
It's almost working... the problem is I'm NOT getting the old left text and the old right text shown. I'm getting \1 and \2.
What's wrong?
Many thanks.
|
|
|
|
|
This is a different approach, maybe it works for you?
string regex = @"[\r\n]{1,2}(.+)\#(.*)[\r\n]{1,2}";
Match m = Regex.Match(input, regex);
if (m.Success)
{
string output = String.Format("{0}{1}{2}{3}{4}\r", myRightVar, m.Groups[1], myCenterVar, m.Groups[2], myLeftVar);
Console.WriteLine(output);
}
Probably this might also work, it's way shorter:
string output2 = Regex.Replace(input, regex, String.Format("{0}$1{1}$2{2}\r", myLeftVar, myCenterVar, myRightVar));
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Thanks a lot but it didn't work for me
The string I am parsing contains the following:
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla.
toto toto toto # toto toto toto
toto toto toto # toto toto toto
toto toto toto # toto toto toto
toto toto toto # toto toto toto
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla.
So I want to add alter the content on the following way:
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla.
[myLeftVar]toto toto toto [myCenterVar] toto toto toto[myRightVar]
[myLeftVar]toto toto toto [myCenterVar] toto toto toto[myRightVar]
[myLeftVar]toto toto toto [myCenterVar] toto toto toto[myRightVar]
[myLeftVar]toto toto toto [myCenterVar] toto toto toto[myRightVar]
And so on...
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla.
Please help.
|
|
|
|
|
This should be it.
string regex=@"(.+)\#(.+)\r\n";
string replace=String.Format("{0}$1{1}$2{2}\r\n","Left","Center","Right");
string output=Regex.Replace(input,regex,replace);
I've tweaked the regex as I was having problem with it.
I've dumped the code I used to test it:
using System;
using System.Text.RegularExpressions;
public class Testy
{
public static void Main()
{
string input="bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla.\r\n\r\n"+
"toto toto toto # toto toto toto\r\n"+
"toto toto toto # toto toto toto\r\n"+
"toto toto toto # toto toto toto\r\n"+
"toto toto toto # toto toto toto\r\n\r\n"+
"bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla";
string regex=@"(.+)\#(.+)\r\n";
string replace=String.Format("{0}$1{1}$2{2}\r\n","Left","Center","Right");
string output=Regex.Replace(input,regex,replace);
Console.WriteLine(output);
}
}
|
|
|
|
|
Thanks a lot Graham it worked
|
|
|
|
|
Good to know.
Regexs are good fun, but can be a pain in the a**e sometimes!!
|
|
|
|
|
I have this left
$content = preg_replace('#[color=([^-]+)-(.*)[/color]#Uis', '$2', $content);
This is simply to parse bbcode color tag, e.g. [color=red]text[/color]
Please help.
|
|
|
|
|
Hi
using System;
using System.Text.RegularExpressions;
public class Testy
{
public static void Main()
{
string input="This is a [color=red]red[/color] color and this is a [color=blue]blue[/color] color.";
string regex=@"\[color=[^\]]*?\](.*?)\[/color\]";
string output=Regex.Replace(input,regex,"$1");
Console.WriteLine(output);
}
}
There was a couple of things wrong with the regex. The original was greedy, and is now lazy. Also some escaping of ']' and '[' was needed.
Graham
-- modified at 14:41 Monday 26th June, 2006
|
|
|
|
|
Thanks a lot but what if I want to change the brakets type, say:
-color=red- text text -color!-
How can I parse it to:
<font color="red"> text text </font>
|
|
|
|
|
string regex=@"\[color=([^\]]*?)\](.*?)\[/color\]";
string replace=@"<color=$1>$2</color>";
string output=Regex.Replace(input,regex,replace);
If you are intending to place bbcode for a certain number of tags, it might be easier to use a generic regex.
-- modified at 17:58 Monday 26th June, 2006
|
|
|
|
|
Many thanks, It worked, but not when it's nested:
text text text text [color=red]hello [color=green]ok[/color] test ok[/color]
Is there an easy solution for this?
|
|
|
|
|
The easy way would be to treat the opening and closing tags independently of each other.
using System;
using System.Text.RegularExpressions;
public class Testy
{
public static void Main()
{
string input="This is a [color=red]red sentence with [color=blue]some blue[/color] words[/color].";
input=Regex.Replace(input,@"\[color=([^\]]*?)\]","<color=$1>");
input=Regex.Replace(input,@"\[/color\]","</color>");
Console.WriteLine(input);
}
}
The hard way would involve some complex regexs. Will try to come up with something.
Graham.
[Edit]
Hmmm. It appears that this might be pretty difficult. The problem is that regexs by definition match up by looking forward. To look for nested tags, requires searching the text in tree-wise fashion. To do this in linear text requires requires bilaterial searching, looking forward from the left at the same time looking backwards from the right.
Problem is that regexes are virtually impossible to do proper backwards searching. It may be possible to achieve the same effect with some creative code along side regexs.
-- modified at 19:13 Monday 26th June, 2006
|
|
|
|
|
Worked, again
Which one is better for the closing tab in your opinion:
This:
output = input.Replace("[/color]", "</color>");
Or this:
input=Regex.Replace(input,@"\[/color\]","</color>");
The first one is faster right?
I PHP normal function is faster than regular expression, is it the samething in C#?
-- modified at 19:16 Monday 26th June, 2006
|
|
|
|
|
They both do the same job. The first it a straight substitution, the second a regex. The first would be marginally faster to perform.
Have you read my edit on the harder solution?
|
|
|
|
|
I just did. Anyway, the easy method is more than enough for me.
I really appreciate your help.
|
|
|
|
|
That's okay. That's my alotment of regexs used up for the night!
|
|
|
|
|
Can I deserialize XML into an object by reverse-engineering the class definition from a sample XML document, if I don't have the actual class definition from which the XML was serialized?
I'm making function calls to the Google Maps API geocoder using an HTTP request. The function call returns an XML file, which I want to extract data from.
Here are the first few lines of the XML file:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.0">
<Response>
<name>1600 Amphitheatre Parkway, Mountain View,CA</name>
....
And here are the first few lines of the class definition that I reverse-engineered from the XML:
[XmlRootAttribute(ElementName = "kml", IsNullable = false)]
public class kmlJSON
{
public kmlJSON() { }
public class Response
{
public String name;
....
Here is my C# routine to deserialize the data into an object of kml class:
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream resStream = response.GetResponseStream();
XmlSerializer xs = new XmlSerializer(typeof(kmlJSON));
kmlJSON jsonObject = new kmlJSON();
jsonObject = (kmlJSON)xs.Deserialize(resStream);
When I run it, I get the following error at jsonObject = (kmlJSON)xs.Deserialize(resStream); :
[InvalidOperationException: <kml xmlns='http://earth.google.com/kml/2.0'> was not expected.]
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderkmlJSON.Read3_kml() +91
[InvalidOperationException: There is an error in XML document (1, 40).]
Any ideas why XmlDeserializer is getting stuck on the root declaration of the XML file? Am I missing something simple, or must I have the exact class definition that the XML was serialized from?
|
|
|
|
|
Are you sure that the XML is the result of serializing an object?
---
b { font-weight: normal; }
|
|
|
|
|
No, I'm not sure, and it probably wasn't. In fact, most likely it was generated by another language, like php or javascript. I didn't realize that was an issue. I figured that since an XML file was just text in XML format, with no special notation to indicate that it was serialized, it wouldn't matter whether it was serialized or generated by another method. As long as the class definition matched the XML format, I couldn't see how the deserializer would know any better. But I'm new to all this.
In any event, I wound up parsing the XML with XmlTextReader, and it wasn't too hard since I'm only extracting one node right now. But if I wanted to extract multiple nodes, or a whole data record, deserialization would be a better solution if it were possible. Is it?
|
|
|
|
|
It's probably not possible to deserialize the XML straight off, but might be done if you make some adjustments to it first.
You can try to create a class that matches the data in the XML, and serialize it to see what the XML should look like to be deserializable.
---
b { font-weight: normal; }
|
|
|
|
|
That's a good idea, thanks. I already built a class that I think matches the XML. Should be simple to serialize an object and compare it to the XML I'm trying to deserialize.
|
|
|
|
|
Hi all,
I'm writing a Web Application with C#. In this I have a DataSet which I've setup to return the user's role via a Table Adapter. So far I have this code:
UserDataTableAdapters.aspnet_RolesTableAdapter myAdapter = new UserDataTableAdapters.aspnet_RolesTableAdapter();<br />
<br />
txtRoleName.Text = myAdapter.GetRoleFromUserName("Martin").Rows[0].ItemArray[0].ToString();
Which works... but given DataSets are supposed to make data access easier, it seems a little bit clunky to me (as GetRoleFromUserName returns a DataTable, which I'm having to cast to a string after getting at the value I really want via the Rows and Items arrays. All I really want here is return the String RoleName - is there a better way of using TableAdapters and DataSets in code to return a single value?
Thanks,
Martin.
-- modified at 10:56 Sunday 25th June, 2006
|
|
|
|
|
Utini wrote: is there a better way of using TableAdapters and DataSets in code to return a single value?
No, but there is if you don't use them .
The IDbCommand interface (and thus all implementing classes) has a ExecuteScalar function which you can use to fetch a single value.
|
|
|
|
|
There's no real good place to post this, but I'm having trouble with a SQL query:
SELECT Task.Tasknumber,ScheduleItem.HoursScheduled / HourTotals.HoursScheduled AS PlannedValue, (ScheduleItem.HoursScheduled / HourTotals.HoursScheduled * 1) AS EarnedValue<br />
FROM ScheduleItem INNER JOIN (ScheduleItem.TaskId = Task.TaskId) CROSS JOIN HourTotals<br />
GROUP BY Task.TaskId,Task.Tasknumber,ScheduleItem.HoursScheduled,HourTotals.HoursScheduled
I want the value of EarnedValue to be zero of HoursNeeded in ScheduleItem is higher than zero. Anyone know a trick in SQL to do this? Or should I make that function in C#?
WM.
What about weapons of mass-construction?
|
|
|
|
|