Wrox Programmer Forums
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 5th, 2004, 03:13 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
Default Regular Expression issue

I am trying to do this with a Reg Ex:

I have the following two lines (seperated by a CR/LF) that I am trying to combine into one string.

   Start with this:
%_N_BORE1_F3_2422_SPF
;$PATH=/_N_WKS_DIR/_N_K83_204322422_WPD

   Make Into this:
WKS_DIR/K83_204322422.WPD/BORE1_F3_2422.SPF

Note: The "_N_" prefix has been stripped, and the "_" in "_WPD" and "_SPF" has been switched ot a ".".

Any ideas?
__________________
Mitch
 
Old May 6th, 2004, 03:46 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Mitch,

This should do the trick...

var str = "%_N_BORE1_F3_2422_SPF" + "\r\n" + ";$PATH=/_N_WKS_DIR/_N_K83_204322422_WPD";
var exp = new RegExp("^%_N_(.*)_SPF\r\n\;[\$]PATH=\/_N_(.*\/)_N_(.*)_WPD$");
alert(str.replace(exp, "$2$3.WPD\/$1.SPF"));

Best regards,

Chris

 
Old May 6th, 2004, 08:49 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hey thanks, I'll try to put it to use.

In the following line:

    alert(str.replace(exp, "$2$3.WPD\/$1.SPF"));

I take it that this is not a Regular Expression, but a Java function.

Is there no way to do the replace with a Regular Expression?
 
Old May 6th, 2004, 09:01 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hey Mitch,

the first line defines your source string:

var str = "%_N_BORE1_F3_2422_SPF" + "\r\n" + ";$PATH=/_N_WKS_DIR/_N_K83_204322422_WPD";

the second defines a regular expression:

var exp = new RegExp("^%_N_(.*)_SPF\r\n\;[\$]PATH=\/_N_(.*\/)_N_(.*)_WPD$");

the last line uses the string object's replace method using the regular expression as the first argument, the $1 / $2 / $3 parts are just shortcuts to the regular expressions submatches:

alert(str.replace(exp, "$2$3.WPD\/$1.SPF"));

So the replace is done using the source string & regular expression only - no Java required.

HTH,

Chris

 
Old May 6th, 2004, 09:12 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks!

I will put it to good use! :)
 
Old May 8th, 2004, 11:34 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well it worked but then my app that was using it didn't give me what I needed, so now I am doing it all external to my app. So here is what I am now faced with.

I am trying to process a directory of files that need to be renamed and relocated.

I will have in a temp directory several files that will all have the info needed to rename and move them in the first couple of lines. I used a regular expression to determin the file name and directory but not sure how use it on each file and how to loop through all the files in the directory.

Each file will look something like this:

%_N_STAMP__SPF
;$PATH=/_N_WKS_DIR/_N_L83_204322419_WPD
N5 G0 X-20 Y36
N10 G0 Z10
N15 G1 Z-10
N20 G0 Z250
N25 M17

Each file starts with the following 2 lines:

%_N_xxx
;$PATH=/_N_WKS_DIR/_N_yyy

The xxx is the file name, the yyy is the directory it goes in.

My Pattern = "%_N_([0-9A-Za-z_]+)\s*;\$PATH=/_N_WKS_DIR/_N_([0-9A-Za-z_/]+)"

Which should give me the file name as \1 and the directory it goes in as \2.

1. My first issue is how to loop thru the files and open each one.
2. My second issue is how to apply the reg ex pattern to each file. Not sure if I need to go line by line or if I should pass the entire file to the expression.
3. My third is how to retrieve the results of the reg ex, the \1 and \2
4. My fourth issue is how to then save the file out using the \1 and \2 returned by the reg ex.

I think that is all.

I am doing it in VB or Java Script since the application that I was using that creates all the files would only allow me to use my reg ex (within the app itself) on the first match it found. I am now just using the app to break appart all the individual files from one big file, which it does well (its a special serial communication program). I could run this script on the big file itself but thought that would be even more difficult, so I let the app break apart each file and then I am trying to process what it creates so I can name them and put them in the correct directory.

Here is what I have so far, but it is not much.

Option Explicit


'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''
' Constants for opening files
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''
Const OpenFileForReading = 1
Const OpenFileForWriting = 2
Const OpenFileForAppending = 8

Const strTempDir="C:\My Documents\VB_Temp_Holding\"

GetFile 'Call function

'********************************
Function GetFile()

  Dim TextStream
  Dim S
  Dim File
  dim fso
dim TabStop
dim NewLine

TabStop = Chr(9)
NewLine = Chr(10)

'*********************************
Dim inputstr, re, amt
Set re = new regexp 'Create the RegExp object

re.Pattern = "%_N_([0-9A-Za-z_]+)\s*;\$PATH=/_N_WKS_DIR/_N_([0-9A-Za-z_/]+)"
re.IgnoreCase = true

'***************************

'Create a FileSystemObject
set fso = CreateObject("Scripting.FileSystemObject")


     Set TextStream = FSO.OpenTextFile(strTempDir & "test.txt", OpenFileForReading)
  Set File = FSO.GetFile(strTempDir & "test.txt")
  Set TextStream = File.OpenAsTextStream(OpenFileForReading)
  Do While Not TextStream.AtEndOfStream
     S = S & TextStream.ReadLine & NewLine
  Loop
  TextStream.Close

  GetFile= S
'msgbox "The file says: " & s

re.Test(s)
re.Global = True

End Function





Similar Threads
Thread Thread Starter Forum Replies Last Post
regular expression MunishBhatia ASP.NET 2.0 Professional 5 May 22nd, 2007 07:59 AM
REGULAR EXPRESSION pallone Javascript How-To 0 September 5th, 2006 06:52 AM
need regular expression keyvanjan ASP.NET 1.0 and 1.1 Basics 5 August 31st, 2006 07:16 AM
Regular Expression Help anshul PHP How-To 4 December 8th, 2004 05:19 AM
help in regular expression diby Beginning PHP 1 September 17th, 2003 12:25 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.