473,761 Members | 1,808 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regular expression to exclude lines?

Sorry to ask what is surely a trivial question. Also sorry that I don't have
my current code version on hand, but... Anyway, must be some problem with
trying to do the negative. It seems like I get into these ruts each time I
try to deal with regular expressions.

All I'm trying to do is delete the lines which don't contain a particular
string. Actually a filter to edit a log file. I can find and replace a thing
with null, but can't figure out how to find the lines which do not contain
the thing.

Going further, I want to generalize and use a JavaScript variable containing
the decision string, but first I need to worry about the not-within-a-line
problem.

Jul 20 '05
26 11793
Try this to exclude lines that don't have "something" in them:

rx = /^(?:(?!\bsometh ing\b).)*$/gm;
output = input.replace(r x,'');

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #11
JRS: In article <3f************ *********@news3 .asahi-net.or.jp>, seen
in news:comp.lang. javascript, Shannon Jacobs <sh****@my-deja.com> posted
at Mon, 24 Nov 2003 21:50:22 :-
All I'm trying to do is delete the lines which don't contain a particular
string. Actually a filter to edit a log file. I can find and replace a thing
with null, but can't figure out how to find the lines which do not contain
the thing.


Of course, for a *particular* string, not requiring a RegExp, DOS batch
provides the answer, and there must surely be a UNIX equivalent.

find "mystring" < old.log > new.log

It seems likely that someone has written a version of DOS find that
accepts RegExps; given RegExp code in library form, the job seems
trivial. UNIX has grep, which should do; and there are ports of grep to
DOS & Windows. Also, WSH has file I/O and RegExps, AIUI.

The important thing seems to be to not bother with a RegExp
substitution, but to work line-by-line and do a RegExp (or other) test
of acceptability.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk DOS 3.3, 6.20; Win98. ©
Web <URL:http://www.merlyn.demo n.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demo n.co.uk/programs/00index.htm>
My DOS <URL:http://www.merlyn.demo n.co.uk/batfiles.htm> - also batprogs.htm.
Jul 20 '05 #12
Dr John Stockton wrote:
Of course, for a *particular* string, not requiring a RegExp, DOS batch
provides the answer, and there must surely be a UNIX equivalent.

find "mystring" < old.log > new.log


grep -v 'mystring' old.log >new.log 2>&1

The single quotes are only required if special shell expressions are
used but not escaped. 2>&1 captures (error) messages in new.log, too.
If you do not want that, leave it out.
PointedEars
Jul 20 '05 #13
JRS: In article <3F************ **@PointedEars. de>, seen in
news:comp.lang. javascript, Thomas 'PointedEars' Lahn
<Po*********@we b.de> posted at Wed, 26 Nov 2003 17:27:13 :-
Dr John Stockton wrote:
Of course, for a *particular* string, not requiring a RegExp, DOS batch
provides the answer, and there must surely be a UNIX equivalent.

find "mystring" < old.log > new.log


grep -v 'mystring' old.log >new.log 2>&1

The single quotes are only required if special shell expressions are
used but not escaped. 2>&1 captures (error) messages in new.log, too.
If you do not want that, leave it out.


I'm not aware of 2>&1 being valid in either DOS or Win98, and GREP is
not part of those systems but must be imported.

Why did you cut the part where I wrote "UNIX has grep, which should do;
and there are ports of grep to DOS & Windows." ?

IMHO, MiniTrue is more useful than GREP and SED; see my reply to your
earlier post.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demo n.co.uk/> - FAQish topics, acronyms, & links.
I find MiniTrue useful for viewing/searching/altering files, at a DOS prompt;
free, DOS/Win/UNIX, <URL:http://www.idiotsdelig ht.net/minitrue/> Update soon?
Jul 20 '05 #14
Dr John Stockton wrote:
Thomas 'PointedEars' Lahn wrote:
Dr John Stockton wrote:
Of course, for a *particular* string, not requiring a RegExp, DOS batch
provides the answer, and there must surely be a UNIX equivalent.

find "mystring" < old.log > new.log
grep -v 'mystring' old.log >new.log 2>&1

The single quotes are only required if special shell expressions are
used but not escaped. 2>&1 captures (error) messages in new.log, too.
If you do not want that, leave it out.


I'm not aware of 2>&1 being valid in either DOS or Win98,


It is only available in Cmd.exe of Windows NT-based systems and
Unices, generally saying in POSIX-compatible shells, of course.
and GREP is not part of those systems but must be imported.
Of course. My posting was only regarding "there must
surely be a UNIX equivalent". Here you are :)
Why did you cut the part where I wrote "UNIX has grep, which should do;
and there are ports of grep to DOS & Windows." ?
Just oversaw it.
IMHO, MiniTrue is more useful than GREP and SED; see my reply to your
earlier post.


MiniTrue is not part of a basic installation of Unices,
though. (Instead, mtr refers to Matt's Traceroute.)
F'up2 poster

PointedEars
Jul 20 '05 #15
JRS: In article <3F************ **@PointedEars. de>, seen in
news:comp.lang. javascript, Thomas 'PointedEars' Lahn
<Po*********@we b.de> posted at Thu, 27 Nov 2003 01:27:41 :-
Dr John Stockton wrote:
Thomas 'PointedEars' Lahn wrote:
grep -v 'mystring' old.log >new.log 2>&1

The single quotes are only required if special shell expressions are
used but not escaped. 2>&1 captures (error) messages in new.log, too.
If you do not want that, leave it out.


I'm not aware of 2>&1 being valid in either DOS or Win98,


It is only available in Cmd.exe of Windows NT-based systems and
Unices, generally saying in POSIX-compatible shells, of course.


There is no reason to assume that the OP is aware of that; or even of
that part of that that is applicable to the system in question. a
plausible but inapplicable or incorrect "solution" is worse than
useless".

IMHO, MiniTrue is more useful than GREP and SED; see my reply to your
earlier post.


MiniTrue is not part of a basic installation of Unices,
though. (Instead, mtr refers to Matt's Traceroute.)


Indeed; nor of DOS; which is sufficiently clearly indicated by my
signature to that article.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME ©
Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
Web <URL:http://www.merlyn.demo n.co.uk/news-use.htm> : about usage of News.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Jul 20 '05 #16
Dr John Stockton wrote:
Thomas 'PointedEars' Lahn wrote:
Dr John Stockton wrote:
Thomas 'PointedEars' Lahn wrote:
grep -v 'mystring' old.log >new.log 2>&1

The single quotes are only required if special shell
expressions are used but not escaped. 2>&1 captures (error)
messages in new.log, too. If you do not want that, leave it
out.

I'm not aware of 2>&1 being valid in either DOS or Win98,
It is only available in Cmd.exe of Windows NT-based systems and
Unices, generally saying in POSIX-compatible shells, of course.


There is no reason to assume that the OP is aware of that;


There is no reason that she is interested in it, either.
or even of that part of that that is applicable to the system in
question.
The system in question is unknown.
a plausible but inapplicable or incorrect "solution" is worse than
useless".


This is a JavaScript newsgroup, not a shell script newsgroup. As the
OP has not provided the system on which solutions are supposed to work,
she will test if the provided solutions will work on her system.
PointedEars
Jul 20 '05 #17
Thomas 'PointedEars' Lahn wrote:
Dr John Stockton wrote:

<snip>
There is no reason to assume that the OP is aware of that;


There is no reason that she is interested in it, either.

<snip>

Return of the original poster... Sorry I've been rather busy and haven't
been able to follow this very interesting thread more closely. (The OP is
male, by the way.) However, judging by the complexity of the discussion, it
seems that there was some reason for my original perplexity, though I
thought it a trivial notion.

First let me try to clarify what I'm doing. I know that JavaScript has no
access to the file system. The files are to be handled directly by the user
of the utility. In the Windows environment, this is trivial with ^A, ^C, and
^V. I didn't mention that part because it's almost mindless now (for me).
The actual steps for the file part are:

Open the converter JavaScript form, then open the target file, ^A, ^C, click
in the form, ^V, click on the convert button of the form, ^A, ^C, click back
in the original file, ^A, ^V, and save the file. Done. (If anyone is curious
and the results of these steps are not obvious enough, I can explain.)

Now to clarify the JavaScript part. This example is from an existing utility
that converts raw HTML into JavaScript. The variable HtmlText is the body of
the file from an input field in the form. The critical function is:

function jsFromHtml(Html Text) {
HtmlText = HtmlText.replac e(/\"/g,"\\\"");
HtmlText = HtmlText.replac e(/[\r\n]+/g,"\");\r\ndocu ment.writeln(\" ");
return "document.write ln(\"" + HtmlText + "\");";
}

I know this is rather ugly code, and I'd also be interested in improvements,
or even a completely different approach. My JavaScript skills are obviously
rather limited, but this was adequate for my purposes at the time. Since
it's probably not IOttMCO, I'll explain what it does. In the first
executable line, the regular expression escapes all of the double quotes in
the original HTML. In the next line, all of the embedded line breaks are
replaced with the end and start of document.writel n statements, and then the
last line puts one more start and end around the entire thing. The result is
a block of JavaScript code which outputs the arbitrary HTML input. You stick
that into a JavaScript function to create that block of HTML under program
control wherever it is required. (I was especially unhappy with my treatment
of line breaks, and believe this is not a properly general method, though it
works.)

My goal now is to do something similar, but excluding the lines that do not
contain some string. I'm most interested in an elegant solution, though the
discussion so far seems to suggest that there may be no better approach than
parsing the input one line at a time...

An additional wrinkle is that I'd like to generalize a bit by treating the
decision string as a parameter returned in another field of the form.

Jul 20 '05 #18
"Shannon Jacobs" <sh****@my-deja.com> writes:
First let me try to clarify what I'm doing. I know that JavaScript has no
access to the file system. The files are to be handled directly by the user
of the utility. In the Windows environment, this is trivial with ^A, ^C, and
^V.
I have pages like that too, for colorizing HTML and Javascript :)
Now to clarify the JavaScript part. This example is from an existing utility
that converts raw HTML into JavaScript. The variable HtmlText is the body of
the file from an input field in the form. The critical function is:

function jsFromHtml(Html Text) {
HtmlText = HtmlText.replac e(/\"/g,"\\\"");
HtmlText = HtmlText.replac e(/[\r\n]+/g,"\");\r\ndocu ment.writeln(\" ");
return "document.write ln(\"" + HtmlText + "\");";
} I know this is rather ugly code, and I'd also be interested in improvements,
or even a completely different approach.
I would use split:

function jsFromHtml(Html Text) { // I would write HTML in all caps :)
var inputLines = HtmlText.split(/[\r\n]+/);
var outputLines = [];
for (var i=0;i<inputLine s.length;i++) {
var safeLine = inputLines[i].replace(/[\\"]/g,"\\$&");
outputLines[i] = "document.write ln(\"" + safeLine + "\");" ;
}
return outputLine.join ("\n");
}

(I put a backslash in front of both double quotes and backslashes,
since neither can occour alone in a string. If there are other
characters that makes no sense in a string, they should be
handled as well. Examples could be \t or \b).
My JavaScript skills are obviously rather limited,
Not obviously. It works, and it's something I could find myself doing if
I didn't have split available. It's possibly even faster to change all
the quotes from the beginning instead of doing one replace per line.
but this was adequate for my purposes at the time. Since it's
probably not IOttMCO,
You lost me there :) IOttMCO?
I'll explain what it does.
It's fairly easy to read, as long as you can see what's inside a string
and what's not :)
My goal now is to do something similar, but excluding the lines that do not
contain some string. I'm most interested in an elegant solution, though the
discussion so far seems to suggest that there may be no better approach than
parsing the input one line at a time...
Nothing wrong with one line at a time. If you use the code I showed above,
all you need is to wrap the content of the for loop in an if statement:

if (!/badWord/.test(inputLine s[i])) {
... add to output ...
}

or

if (inputLines[i].indexOf("badWo rd") == -1) {
... add to output ...
}

An additional wrinkle is that I'd like to generalize a bit by treating the
decision string as a parameter returned in another field of the form.


var testRE = RegExp(form.ele ments['otherField'].value);
if (! testRE.test(inp utLines[i])) {
...
}

(To avoid problems or crashes, you might want to screen the other field's
values for characters that are meaningful in regular expressions)
or

var testWord = form.elements['otherField'].value;
if (inputLines[i].indexOf(testWo rd)==-1) {
... add to output ...
}
Good luck
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #19
Lasse Reichstein Nielsen wrote:
"Shannon Jacobs" <sh****@my-deja.com> writes:
but this was adequate for my purposes at the time. Since it's
probably not IOttMCO,


You lost me there :) IOttMCO?


Intuitively Obvious to the Most Casual Observer

http://babylon.com/ (the tool) or http://online.babylon.com/combo/
(the website) are sometimes quite handy :)
HTH

PointedEars
Jul 20 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2434
by: dreamerbin | last post by:
Hi, I'm having trouble extracting substrings using regular expression. Here is my problem: Want to find the substring that is immediately before a given substring. For example: from "00 noise1 01 noise2 00 target 01 target_mark", want to get "00 target 01"
4
5171
by: Buddy | last post by:
Can someone please show me how to create a regular expression to do the following My text is set to MyColumn{1, 100} Test I want a regular expression that sets the text to the following testMyColumn{1, 100}Test Basically I want the regular expression to add the word test infront of the
4
3229
by: Neri | last post by:
Some document processing program I write has to deal with documents that have headers and footers that are unnecessary for the main processing part. Therefore, I'm using a regular expression to go over each document, find out if it contains a header and/or a footer and extract only the main content part. The headers and the footers have no specific format and I have to detect and remove them using a list of strings that may appear as...
2
8566
by: Dan Schumm | last post by:
I'm relatively new to regular expressions and was looking for some help on a problem that I need to solve. Basically, given an HTML string, I need to highlight certain words within the text of the string. I had it working somewhat, but ran into problems if one of the highlighted words could also be part of an HTML tag (such as 'Table' or 'Border'). What I need is the regex to find the word, but ignore any words that fall between an HTML...
1
1227
by: Jim Heavey | last post by:
I created a regular expression and I used the following regular expression: {1,1000} I used \091 and \093 to indicate that "" are valid characters, but it seems to ignore those values as it will not allow me to enter those values into the field where this reqular expression is associated. How can I tell it to allow these characters to be keyed? Also, I tried to use a bind expression such as ValidationExpression=<%#
3
2566
by: Zach | last post by:
Hello, Please forgive if this is not the most appropriate newsgroup for this question. Unfortunately I didn't find a newsgroup specific to regular expressions. I have the following regular expression. ^(.+?) uses (?!a spoon)\.$
25
5166
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART (CONDUCTION DEFECT) 37.33/2 HEART (CONDUCTION DEFECT) WITH CATHETER 37.34/2 " the expression is "HEART (CONDUCTION DEFECT)". How do I gain access to the expression (not the matches) at runtime? Thanks, Mike
1
1767
by: othellomy | last post by:
I am trying to exclude all strings that has 'a' inside (I have simplified the actual problem) select 1 where 'bb b a dfg' like '%%' However, the above does not work. By the way, I can not use 'not like' such as: select 1 where 'bb b a dfg' not like '%a%'
14
4991
by: Andy B | last post by:
I need to create a regular expression that will match a 5 digit number, a space and then anything up to but not including the next closing html tag. Here is an example: <startTag>55555 any text</aClosingTag> I need a Regex that will get all of the text between the html tags above (the html tags are random and i do not know them before hand). The match string always starts with at least 5 digits.
0
9554
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9925
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9811
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8814
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7358
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5266
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3913
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.