Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with Regex

Status
Not open for further replies.

rf222

Programmer
Joined
Jan 29, 2004
Messages
94
Location
US
I have executed the following regex:

- (PNMIS_)(\S*\S*\S*\S*\S*\S*\S*\S*\S*\S*\S*\S*\S*\S*\S*\S*\S*)\S*_REPORT

Replacement:

- pr1.tss.mis.$2.c


Sample data:

/* ----------------- PNMIS_test6732323323_REPORT ----------------- */
/* ----------------- PNMIS_test23_REPORT ----------------- */

The systems hangs! I need 17 characters on position $2 only. Can not have more.

Please help. Thanks
 
> I need 17 characters on position $2 only.

Your description is ambiguous and your pattern does not satisfy any of the sample?! Try this?
[tt]
var rx=new RegExp("(PNMIS_)(\\S{17})\\S_REPORT")
var s="/* ----------------- PNMIS_test6732323323ssst_REPORT ----------------- */";
alert(RegExp.$2);
[/tt]
 
I missed out a line! Here is a re-take.
[tt]
var rx=new RegExp("(PNMIS_)(\\S{17})\\S_REPORT")
var s="/* ----------------- PNMIS_test6732323323ssst_REPORT ----------------- */";
var cm=rx.exec(s);
alert(RegExp.$2);
[/tt]
 
Thanks. I will try it today. Why is there "\\S". I thought "\S" was enough. please explain. Thanks. "\S" means one no space character. I did not know about {n} syntax... Great help
 
The format used here to establish regexp will not recognize "\" as literal, it will take it as escape character. If you use another format, say,
[tt]/PNMIS_)(\S{17})\S_REPORT/[/tt]
then you don't need to. You didn't reveal what approach you took.
 
My choice of words may not be perfect, upon re-reading it, you get the meaning...
 
Shoul not this be:
/PNMIS_)(\S{17})\S*_REPORT/

What {17} means? First 17 characters? What if there are only 5 characters after "PNMIS_" ? Example: "PNMIS_test1_REPORT.

By the way, the other regex finished sucessfully, yet it took 10 hours to run!
 
{x} - matches exactly x instances of the preceding character

If that does not work for you, instead you could use this terminology:
{x,y} - matches at least x and at most y instances of the preceding character

So if you wanted to match anywhere between 0 and 17 \S, then you would do it like this:
(take note that there is no space between the 0, the comma, and the 17 - that is very important as it will not work if you have any spaces included)
Code:
/PNMIS_)(\S{0,17})\S*_REPORT/

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
The /.../ expression, I had missed out an open parenthesis during cut and paste operation. I guess it would be obvious. Hence, it should be read /[highlight]([/highlight]PNMIS_).../.

The {17} is what I understand from what you said I need 17 characters on position $2 only, I quoted already.

>it took 10 hours to run
No idea what you have been doing...
 
It works fast now! Great, thank you!

I wonder, maybe you can help me extend this, I have a feeling I need to do lookbehind, but not sure how.

Sample Data:

----------START OF DATA ---------------

/* ----------------- PNMIS_MSTANDARD_REPORT ----------------- */

insert_job: PNMIS_MSTANDARD_REPORT job_type: c
condition: success(PNMIS_NYMONTHLY2_REPORT)

/* ----------------- PNMIS_NYMONTHLY_REPORT ----------------- */

insert_job: PNMIS_NYMONTHLY_REPORT job_type: c
condition: success(PNMIS_MSTANDARD_REPORT)

/* ----------------- PNMIS_NYMONTHLY2_REPORT ----------------- */

insert_job: PNMIS_NYMONTHLY_REPORT job_type: c
condition: success(PNMIS_MSTANDARD55_REPORT) and success(PNMIS_THIS_ONE_IS_NOT_IN_THIS_FILE_REPORT)

blah, blah, blah...
----------END OF DATA ---------------


1) So this regex will replace the header:
- PNMIS_(\S{0,17})\S*_REPORT

However I would like to also:

2) Replace also what is after "insert_job", I guess I need to add some or statement at the begining...

3) Replace what is after "success(", however only for entries, which are in the file (header).

So step 3 would be more tricky, I am thinking that I may need to do this in 2 itteriations, by executing step 3 first with look behind feature, than step 1 and 2. I am not sure what the syntax would be... I also wonder if it is possible to do this all in one itteriation only.

Thanks

 
Lookbehind is not yet supported in regexp engine of javascript. You have to use submatch, just like your using .$1 to do that.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top