Regex help

Hi everyone,

I am pretty much a newbie when it comes to using regular expressions.
I guess all the fancy syntax is pretty much confusing me all the time.

Can someone help me out with an example for the following.

I have the following strings in a file, more like a few hundred

"<record><na>Test Data Form</><t>W</><d></><ph"

1. From every line that I encounter, I want to extract the "Test Data 
Form" from the string.  Basically the value that lie inside the "> 
<".  How would I do that using Regex() ?

Also I have a second question.  I ended up doing a search and replace
using notepad on the file, but after I was finished replacing what I 
did not need in the file, I was left with.  "Test Data Form,"

I ended up putting a comma on the last instances of the string 
containing "</><t>" this value.  That ended up leaving me with another 
question with regex().

2. How would you scan lets say a few hundred strings in a file and 
when you get to the "," or ", " you would delete everything from that 
point onwards so you will only be left with what you need.

old string: "Test Data Form, xy1234"

new string:  "Test Data Form"



Thanks for assisting me with this, it confuses me a lot.



Steven
0
Steven
11/14/2009 9:47:03 PM
dotnet.languages.csharp 1931 articles. 0 followers. Follow

8 Replies
1119 Views

Similar Articles

[PageSpeed] 10

Steven wrote:
> I am pretty much a newbie when it comes to using regular expressions.
> I guess all the fancy syntax is pretty much confusing me all the time.
> 
> Can someone help me out with an example for the following.
> 
> I have the following strings in a file, more like a few hundred
> 
> "<record><na>Test Data Form</><t>W</><d></><ph"
> 
> 1. From every line that I encounter, I want to extract the "Test Data 
> Form" from the string.  Basically the value that lie inside the "> <".  
> How would I do that using Regex() ?

If you explain the rule precise enough, then we can come up with
a regex.

The rules you described would return two strings "Test Data Form"
and "W".

It would be something like "(?:>)([^<]+)(?:<)".

If you explain why "W" should not match then we can probably
find a regex for that.

> Also I have a second question.  I ended up doing a search and replace
> using notepad on the file, but after I was finished replacing what I did 
> not need in the file, I was left with.  "Test Data Form,"
> 
> I ended up putting a comma on the last instances of the string 
> containing "</><t>" this value.  That ended up leaving me with another 
> question with regex().
> 
> 2. How would you scan lets say a few hundred strings in a file and when 
> you get to the "," or ", " you would delete everything from that point 
> onwards so you will only be left with what you need.
> 
> old string: "Test Data Form, xy1234"
> 
> new string:  "Test Data Form"

Why not simply use String IndexOf and String Substring for this?

Arne
0
ISO
11/15/2009 1:04:22 AM
Hi Arne,

Let me see if I can explain what I am trying to do a bit more.

This is my string:

"<record><na>Test Data Form</><t>W</><d></><ph"

I only want to extract the "Test Data Form" from the string, 
everything else I don't need.


I guess since you mentioned the IndexOf() and Substring() functions, I 
will have to try and use those to split the string at whatever char I 
need to split it from.
0
Steven
11/15/2009 1:36:07 AM
Steven wrote:
> Let me see if I can explain what I am trying to do a bit more.
> 
> This is my string:
> 
> "<record><na>Test Data Form</><t>W</><d></><ph"
> 
> I only want to extract the "Test Data Form" from the string, everything 
> else I don't need.

Yes. But what is the criteria for not picking "W" ?

You just want the first ?

> I guess since you mentioned the IndexOf() and Substring() functions, I 
> will have to try and use those to split the string at whatever char I 
> need to split it from.

IndexOf and Substring were for the second problem.

Regex is fine for the first problem.

Arne

0
ISO
11/15/2009 2:37:34 AM
Arne Vajh�j wrote:
> Steven wrote:
>> Let me see if I can explain what I am trying to do a bit more.

> Yes. But what is the criteria for not picking "W" ?
> 
> You just want the first ?

Yes, I just want the first, the other parts of the string is just garbage.
0
Steven
11/15/2009 4:07:39 AM
Steven wrote:
> Arne Vajh�j wrote:
>> Steven wrote:
>>> Let me see if I can explain what I am trying to do a bit more.
> 
>> Yes. But what is the criteria for not picking "W" ?
>>
>> You just want the first ?
> 
> Yes, I just want the first, the other parts of the string is just garbage.

Then the regex is still good. You just call Regex.Match instead of
Regex.Matches.

Arne
0
ISO
11/15/2009 2:46:05 PM
On Nov 14, 11:07=A0pm, Steven <sgra...@hardys.com> wrote:
> Arne Vajh=F8j wrote:
> > Steven wrote:
> >> Let me see if I can explain what I am trying to do a bit more.
> > Yes. But what is the criteria for not picking "W" ?
>
> > You just want the first ?
>
> Yes, I just want the first, the other parts of the string is just garbage=
..

Regex is really hard to do right.  I suggest you go online, get a
bunch of examples, and try it until you get it right.  It takes a
couple of hours, but it's worth it.  Your example is not one of the
trivial 'receipe' examples so to expect somebody here to do the work
is a bit much.

For example, here is a Regex to split words out of a sentence: string
text; string[] words =3D Regex.Split(text, @"\W+");  An excellent source
is the C# book by Albahari et al., which has a chapter on Regex.

Lots of examples on the Net as well.  One thing confusing to me is
whether ">" symbol appears as ASCII in your string.  If it does, it
makes your extraction much easier (search for ascii that's two times
the ">" symbol from the left).

Good luck.

RL
0
RayLopez99
11/15/2009 4:16:45 PM
RayLopez99 wrote:
> On Nov 14, 11:07 pm, Steven <sgra...@hardys.com> wrote:
>> Arne Vajh�j wrote:
>>> Steven wrote:
>>>> Let me see if I can explain what I am trying to do a bit more.
>>> Yes. But what is the criteria for not picking "W" ?
>>> You just want the first ?
>> Yes, I just want the first, the other parts of the string is just garbage.
> 
> Regex is really hard to do right.  I suggest you go online, get a
> bunch of examples, and try it until you get it right.  It takes a
> couple of hours, but it's worth it.  Your example is not one of the
> trivial 'receipe' examples so to expect somebody here to do the work
> is a bit much.

"(?:>)([^<]+)(?:<)" is not that complex ...

Arne

0
ISO
11/16/2009 12:36:45 AM
RayLopez99 wrote:
> On Nov 14, 11:07 pm, Steven <sgra...@hardys.com> wrote:
>> Arne Vajh�j wrote:
>>> Steven wrote:
>>>> Let me see if I can explain what I am trying to do a bit more.
>>> Yes. But what is the criteria for not picking "W" ?
>>> You just want the first ?
>> Yes, I just want the first, the other parts of the string is just garbage.
> 
> Regex is really hard to do right.  I suggest you go online, get a
> bunch of examples, and try it until you get it right.  It takes a
> couple of hours, but it's worth it.  Your example is not one of the
> trivial 'receipe' examples so to expect somebody here to do the work
> is a bit much.
> 
> For example, here is a Regex to split words out of a sentence: string
> text; string[] words = Regex.Split(text, @"\W+");  An excellent source
> is the C# book by Albahari et al., which has a chapter on Regex.
> 
> Lots of examples on the Net as well.  One thing confusing to me is
> whether ">" symbol appears as ASCII in your string.  If it does, it
> makes your extraction much easier (search for ascii that's two times
> the ">" symbol from the left).
> 
> Good luck.
> 
> RL

If you have not downloaded Expresso, do so.  It makes understanding and 
building patterns fairly easy.

-- 
Mike
0
Family
11/16/2009 2:35:39 AM
Reply:

Similar Artilces:

NEED HELP including mileage in invoice ...
i purchased MICROSOFT MONEY 2003 DELUXE AND BUSINESS. NOw, how do I include my mileage in the invoice that I must create in order to be paid from clients. I am self- employed and this information must be entered. I cannot seem to figure out how to do that and the HELP available with the software is NO help! TIA for your assist ... Asking multiple times does not make people who might know the answer more likely to offer to help you. Just the opposite. As for me, I don't use D&B and do **not** answer email requests. "Natalie" <loonsng@aol.com> wrote in message...

HELP Unhide Column A
I have tried to select entire sheet, and adjust columns. I have tried the"go to" select A1 and unhide, BUT column A will not show up.. The sheet is not protected. One possibility is that you moved column A off screen and applied Freeze Panes. If so then remove the freeze panes. -- HTH... Jim Thomlinson "Donna" wrote: > I have tried to select entire sheet, and adjust columns. I have tried the"go > to" select A1 and unhide, BUT column A will not show up.. The sheet is not > protected. try moving putting your cursor on the line ne...

HELP!! NESTING FUNCTION
What do I do to this argument below if when d47 is negative but c47 is positive, how can I add it so that if this is the case........ take g47/d47 Here is what i wrote with error =3DIF(AND(C47<0,D47<0),ABS(G47/D47),IF((C47<0),G47/C47+1,G47/D47),IF((,D47<= 0)=AD, G47/D47+1,G47/D47)) On 30 Jan 2006 05:50:38 -0800, "bullandbear1987" <bullandbear1987@gmail.com> wrote: > What do I do to this argument below if when d47 is negative but c47 is > > positive, how can I add it so that if this is the case........ take > g47/d47 > > >Here is what...

Help with Receipt xml <CONDITION> statement
Hi All, Has anyone used a conditional statement in a receipt to print different comments based on a value in one of the custom text fields. For example I have the CustomText1 field configured in customers and populated with a value to allow customers to be grouped (such as say Retail and Wholesale customers). I wish to print different messages on the receipts based on which group the customer belongs to. I am trying to use the following syntax to determine which message I should print - <CONDITION> Customer.CustomText1 = "Retail" </CONDITION> The expression always se...

Help! Lost secure channel
Hi, Yesterday after a routine reboot, the first Exchange server (there are two) lost it's secure channel with the domain. I can no longer logon with domain credentials, but still can logon locally. There are a couple of fatal schannel errors in the log and users cannot login to their accounts. I've tried using netdom to re-establish the secure channel and although it reports success, I still cannot logon and no mail flows. Does anybody have any ideas to resolve this? What would the implications be of rejoining this server to the domain? It will recreate it's SID, right? Will thi...

Form Field Help line does not show
Hello, I have a medium-size template with a lot (several dozens) of form fields. Some have logical connection, so I'm using ActiveDocument.FormFields("SomeField").Select method to force jump to a specific field on the form. Sometimes this is done from the "exit" macro that does validations, and sometimes to skip some irrelevant fields. I've encountered two issues: 1. The help text in the status line will not show for the proper field in ..Select 2. I can't jump back to the same field. The workaround I found is: .Select a read-only previous fi...

Who can help ??
Something in the list of choices in the special section; When you open the account register and a transaction there is a special section different from the income or expense sections. You find transfers to different accounts in the special section. My list of accounts indicates FTQ and the special section indicates FTQ (contributions) which is wrong. How can I change this ?? My Money version is 2006. Thanks "Dick Watson" <littlegreengecko@mind-enufalready-spring.com> a �crit dans le message de news: u3jquhVIGHA.140@TK2MSFTNGP12.phx.gbl... > We'd love to help, but your ...

Help me run a customer report
I need to know if there is a way to run a report so I may get a list of all customers that purchased products in one of our retail departments to send a flyer too in the future. There's a mailing list report in the Reports Library on Customer Source. -- Glenn Adams Tiber Creek Consulting http://www.tibercreek.com glenn@tibercreek.com ---------------------------------------------- Please DO NOT respond to me directly but post all responses here in the newsgroup so that all can share the information "misshettie" <misshettie@discussions.microsoft.com> wrote in message...

Please help with Time subtracting
I've tried evrything, this is what I need to do: A1 = time in B1 = time out C1 = difference in hours and minutes Now here is the hard part: I want to enter data for A1 and B1 eg. 1234 and have it display 12:34 but not have to use th ":" separator. I have C1 currently formatted for hh:mm with a formula of B1-A1 But I have to constanly hit "shift" ":" when entering data in cells A1, B1. I looked all over the web to no avail, I thought this was a really simple and common excel situation. Any help will be greatly appreciated. Thanks --- Message posted f...

Help! cleaning out inbox and saved emails
I have created in the past two years files in my inbox but want to delete them as they are taking up too much space. How do I transfer these email from my inbox file to a file(s) in Microsoft word. The only way I have found to date is to (Save AS) save each one individually to a word file but this is way too time consumming. Help please. Thanks so much BB Do they have to be in Word? You can create a folder on the Desktop and just drag the messages into it. -- Bruce Hagen MS-MVP [Mail] Imperial Beach, CA "BB" <BB@discussions.micr...

Help: what you see it (not) what you get
Dear MVP's Two questions: What color pallet is publisher 2003 using and more inportant: where can I find it to change on the disk. I have made several publications and every time the background color is not what I get back from the commercial printer services. We use settings in Pub like CMYK, European uncoated as distiller output (we have used addcom prepress from the forum), and NO pantome collorics. I know that some printerservices are making a mess out of it, but we tried several. It is NOT the pictures (they can be set at CMYK) but the use of color. We heve received Joboptions fro...

HELP! Drop Down missing information
I have a customer using an application that has a few drop down combo boxes. The main one is a drop down which is a search and displays 12 pieces of information :File Nbr, Customer, Date, Product, Masterbill, House Bill, Nbr Pieces . . . Suddenly this morning, the user clicks on the drop down, NONE of the customer information appears anymore! NOTHING has been changed with the application. When the user goes to Customer Maintenance, the drop down for it doesn't display either, although there are numerous records in the drop down, none of the info displays. However, just because ...

HELP: Incoming Internet mails delay by the SMTP Connector at Exchange 2003 frontend
We just switched over to Exchange 2003 SMTP frontend and we notice mails always get queued up for 5 to 20 minutes. Force Connection, unfreeze didn't help. I can use telnet to port 25 of the backends to send a message instantly without any delay. So why the mails always get delay by the connector? Thanks Anything configured in the delivery options tab of your connector? James Chong andyhwconnet@hotmail.com wrote: > We just switched over to Exchange 2003 SMTP frontend and we notice > mails always get queued up for 5 to 20 minutes. Force Connection, > unfreeze didn't hel...

Need Mailbox Manager Script
I need a script to run Exchange 2000 Mailbox Manager just one time per month, instead of the weekly or daily options built in to Mailbox Manager. Any ideas, or can someone point me to a pre-made script? I'm a very rank amateur when it comes to scripting, and I need to implement this ASAP! Thanks in advance! ...

Help system broken
I need a way in Outlook 2003 to use the help system. It currently seems incapable of functioning without trying to figure out if I have office 2007, which requires that I enable scripting, which is going to happen whenever hell freezes over or Bill Gates declares that Apple has won, whichever comes first. There is absolutely no reason for help to care. Help text is help text. I don't want my display "optimized", I want the content I paid for. I do not want it going out to the Internet for help, I want to read the local files, and they work perfectly fine in my current browse...

Help sought for IE8.0 to display a web page.
Dear Sir, I am able to view the webpage forum.avira.com with its layout when I am logging on to my PC as 'any other user'. But the web page is not displayed correctly with its layout when I am logging on as 'administator'. It is showing at the end 'errors on the web page' icon. How is this possible? How to get the page displayed when I am logging on as administrator? Thanking you and expecting your posts, Yours sincerely, R. GANESAN. Those with IE7- and/or IE8-specific questions or comments can post to and seek support in IE General newsgroup:...

Outlook keep on asking to change the password. Need help ????
I am keep on getting the following message from outlook when I star to synchronize mails. I logged into hotmail and changed passwords few times. It works first time and again asking me to change password. It just started happening couple of weeks back. It was fine for few years. Never had this problem. Please through in some suggestions ******************************************** Access to the account was denied. Verify that your username and password are correct. Account: 'jktrader', Server: 'http://services.msn.com/svcs/hotmail/httpmail.asp', Protocol: HTTPMail, Ser...

need help choosing the right resource type
A newbie with Project 2007, I'd like advice re how to set up my project resources. We operate as an orchestrator of work done by other companies. Various subcontractors contract with us to provide 'stuff' to us under terms that simplify specify what we're purchasing, when it's to be delivered, and the price. We want to use Project 2007 because there are LOTS of subs involved, and sometimes complex scheduling dependencies between them which we must ride herd on to ensure that the all the pieces of a project come together at a deadline. Seems like we sho...

Early Finish Calculated Incorrectly? Please help soon!
I have a situation where I want to use early start/ early finish dates to represent the planned start/finish of a task by constraining the first task in a series where the project is calculated from the project finish, and everything is allowed to go to ASLAP. Thus the project would be statused relative to all ASLAP or critical dates for summary progress. (Understand that this project will grow to 30,000 lines and this is an easy way to determine critical dates, also the majority of work is in planning package form). The problem is when progress is made on the first task ahead...

Help making a chart that doesn't graph cells without data?
I am setting up a template spreadsheet that will automatically do calculations and graph groups of data into a single scatter plot chart. I have a defined set of 13 groups of between 6 & 150 data points each that I reproducibly collect each day. So I have set up formulae and a chart to automatically calculate and plot as the data are entered. The problem that I have is that each day the number of data points in each group will vary. So I either have too many data points, so some don’t get graphed, or I have too few data points and I get a graph of zeros after the real d...

URGENT !! Please help ~
I cannot configue out this what was happened. My client's CEO only give us this picture http://www.sawed.net/outlook.jpg Sorry..cannot upload to here Anyone can know what happen ? Follow standard Outlook troubleshooting: Run Help | Detect and Repair, = create a new mail profile, and start Outlook in /safe mode. If problems = persist, post back with more relevant information -- such as *when* the = crash occurs and any error messages or other symptoms.=20 --=20 Sue Mosher, Outlook MVP Author of Configuring Microsoft Outlook 2003 http://www.turtleflock.com/olconfig/index.h...

Help on error code 2059
Help on a code error. I have a program transferred from g++ enviroment, it is working fine under linux. But when I use the program in VC++.net, it has the following compiling error: C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(17) : error C2143: syntax error : missing '{' before ':' C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(17) : error C2059: syntax error : ':' C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(17) : error C2143: syntax error : missing '{' before ':' C:\Program Fil...

please help msdn's help
hi, i am now use visual studio.net 2003 with msdn . i move muse to the code 's WinMain function # include <windows.h> int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int icmdShow) { MessageBox(0, "This is my first win32 test!","HelloMsg",MB_OK);//MB_ICONINFORMATION return 0; } a tip shows with content "int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int icmdShow)" and i pressed F1 ,the msdn located the wrong place to CAtlServiceModuleT::WinMain but when i Search the function WinMain msdn will...

combo box help please #2
wow. that site is great. million thanks -- magicwan ----------------------------------------------------------------------- magicwand's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=1478 View this thread: http://www.excelforum.com/showthread.php?threadid=26406 yes, it is ... another site worth a look is www.cpearson.com - look for the link to the topic index at the bottom of the page Cheers JulieD "magicwand" <magicwand.1dbuvy@excelforum-nospam.com> wrote in message news:magicwand.1dbuvy@excelforum-nospam.com... > > wow. > that s...

customizing toolbar help please
I am using Outlook 2000 and everytime I compose a new message the toolbar display some annoying Funtools from Smiley. I tried everything to avoid this I remove it and keeps coming back. Help please!!!!! Thanks Ted Wilson <TedWilson@discussions.microsoft.com> wrote: > I am using Outlook 2000 and everytime I compose a new message the > toolbar display some annoying Funtools from Smiley. I tried > everything to avoid this I remove it and keeps coming back. Try Ad-Aware from http://ww.lavasoftusa.com/ -- Brian Tillman ...