473,840 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Right regex to match -- comments or blank line?

Hi Folks:

I need the right regex to match either a comment line (--) or a blank line.
What I've got below doesn't seem to be working like I'd expect. I've posted
a snippet of what I have below...would anyone be kind enough to point me in
the right direction? I also apparently need to strip the ';' off the end
of the INSERT statement...

TIA,
Dave

-----------------------------------------------------------
while(<SCRIPT>) {

chomp;

$insert = $_;

unless ($insert =~ /^--|^\s+$\n/) {

$rv = $dbh->do($insert);

}

}

The script I'm reading in looks like this:

-- Some comments on this line, the next line is a blank line, then the next
lines are inserts.

insert into sometable values('whateve r1');

insert into sometable values('whateve r2');

etc.


Jul 19 '05 #1
3 12029
jan
chomp(); is removing the \n;

try:
unless ($insert =~ /^--|^\s?$/)

Cheers,
Jan

"Dave Sisk" <ds***@nc.rr.co m.0nospam0> wrote in message news:<0Q******* **************@ twister.southea st.rr.com>...
Hi Folks:

I need the right regex to match either a comment line (--) or a blank line.
What I've got below doesn't seem to be working like I'd expect. I've posted
a snippet of what I have below...would anyone be kind enough to point me in
the right direction? I also apparently need to strip the ';' off the end
of the INSERT statement...

TIA,
Dave

-----------------------------------------------------------
while(<SCRIPT>) {

chomp;

$insert = $_;

unless ($insert =~ /^--|^\s+$\n/) {

$rv = $dbh->do($insert);

}

}

The script I'm reading in looks like this:

-- Some comments on this line, the next line is a blank line, then the next
lines are inserts.

insert into sometable values('whateve r1');

insert into sometable values('whateve r2');

etc.

Jul 19 '05 #2
"jan" <ja*@ossifrage. net> wrote in message
news:fc******** *************** ***@posting.goo gle.com...
chomp(); is removing the \n;


Actually it is removing whatever the value of $/ is. Granted, by default
that is \n, but some people may change it and wonder why chomp() doesn't
work any more.

jue
Jul 19 '05 #3
In article <fc************ **************@ posting.google. com>, jan
<ja*@ossifrage. net> wrote:

[top-posting fixed]
"Dave Sisk" <ds***@nc.rr.co m.0nospam0> wrote in message
news:<0Q******* **************@ twister.southea st.rr.com>...
Hi Folks:

I need the right regex to match either a comment line (--) or a blank line.
What I've got below doesn't seem to be working like I'd expect. I've posted
a snippet of what I have below...would anyone be kind enough to point me in
the right direction? I also apparently need to strip the ';' off the end
of the INSERT statement...

TIA,
Dave

-----------------------------------------------------------
while(<SCRIPT>) {
chomp;
$insert = $_;
unless ($insert =~ /^--|^\s+$\n/) {
Do you want to allow for blanks before the '--'?
Do you require a blank line have whitespace in it?

If "yes" and "no", change to:
unless ( $insert =~ /^\s*--|^\s*$/ ) {

To strip off semi-colon at end:

$insert =~ s/;\s*$//;
$rv = $dbh->do($insert);
}
}

The script I'm reading in looks like this:

-- Some comments on this line, the next line is a blank line, then the next
lines are inserts.

insert into sometable values('whateve r1');

insert into sometable values('whateve r2');

etc.

chomp(); is removing the \n;

try:
unless ($insert =~ /^--|^\s?$/)

Cheers,
Jan


Note: this newsgroup is defunct. Try comp.lang.perl. misc in the future.
Jul 19 '05 #4

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

Similar topics

6
1955
by: Vishant | last post by:
Hi, I'm new to javascript and regEx and trying to solve the following problem. I have a function which validates the password if there is a number: ------------------------------------------------- function findNumeric(str_obj){ regEx = /\d/; if (str_obj.match(regEx))
2
1879
by: Mr.Clean | last post by:
I am working on modifying a syntax highlighter written in javascript and it uses several regexes. I need to add a language to the avail highlighters and need the following regexes modified to parse the new language, Delphi/Pascal. Source to the highlighter is avail here: http://www.dreamprojections.com/SyntaxHighlighter/Default.aspx ********************************************** COMMENTS
2
1871
by: Daniel Billingsley | last post by:
First, if MSFT is listening I'll say IMO the MSDN material is sorely lacking in this area... it's just a whole bunch of information thrown at you and you're left to yourself as to organizing it in your head. Typical learning starts with basics and progresses through increasingly complex information - I think given the inherent confusion-inducing ability of regex that kind of documentation would be very valuable. But anyway, I'm trying...
8
1871
by: rjb | last post by:
Hi! Could somebody have a look and help me to optimize the code below. It may look like very bad way of coding, but this stuff is very, very new for me. I've included just few lines. Regex regxUserName = new Regex(@"(?<=User-Name = )\""(+)\""", RegexOptions.None);
5
10196
by: JackRazz | last post by:
Anyone know the regular expression to match a blank line where the byte sequence is "0D 0A 0D 0A" ive tried "\r\n\r\n+", "^$+" "\n\r" with no success. Any Ideas? Thanks - JackRazz This is the code fragment I'm trying Dim r As Regex, m As Match, i As Integer
8
2597
by: Bob | last post by:
I need to create a Regex to extract all strings (including quotations) from a C# or C++ source file. After being unsuccessful myself, I found this sample on the internet: @"@?""""|@?"".*?(?!\\).""|''|'.*?(?!\\).'" I am inputting the entire source file string and using it with RegexOptions.Singleline. This works OK with, unless the string ends with a back-slash. For example: "This is a test\\". Can anybody see how to fix this...
2
7345
by: beatTheDevil | last post by:
Hey guys, As the title says I'm trying to make a regular expression (regex/regexp) for use in removing the comments from code. In this case, this particular regex is meant to match /* ... */ comments. I'm using Ruby v.1.8.6 Here's my regex: multiline_comments = /\/\*(.*?)\*\// When I try myStr.gsub(multiline_comments, "")
2
2820
by: GS | last post by:
How can one avoid capturing leading empty or blank lines? the data I deal with look like this "will be paid on the dates you specified. xyz supplier amount: $100.52 when: September 07, 2007 reference #: 0415 from: operating account
3
177
by: Jeff | last post by:
I'm parsing this: name="value" and sometimes it looks like this: name2="value2 without the closing '"'. I don't want to capture the end quote.
0
9860
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...
0
10921
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10658
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
10300
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
9437
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...
0
5685
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...
0
5874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4498
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
3138
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.