473,806 Members | 2,321 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

regexp that ignore case or check the case based on a variable

Hello,

I would like to write a regexp that can be either case sensitive or that can
ignore the case based on a variable (value would be either 'i' or '').
For instance in the below code the variable is $case.
I can't make that piece of code working fine. Anyone can help please ?

my $str = "Hello World!";
my $pattern "hello";
my $case = "i";

if ($str =~ s/${pattern}/${case}) {
print "match!\n";
}
Thank you,
Pierre.
Oct 24 '05 #1
2 9490
cyber1
1 New Member
Pierre-

You seem to have some other language mixed in here.
I'm not clear on what it is you want.

But try these:
Expand|Select|Wrap|Line Numbers
  1. my $str = "Hello World!";
  2. my $pattern = "hello";
  3.  
  4. if ($str !~ m/$pattern/g){
  5. print "no match1!\n";
  6. }
  7.  
  8. ##ignore case
  9. if (lc($str) =~ m/$pattern/g){
  10. print "match2!\n";
  11. }
  12.  
  13. ##use case option
  14. if ($str =~ m/$pattern/gi){
  15. print "match3!\n";
  16. }
  17.  
  18.  
More on Perl Regexp:
http://www.pti.co.il/talks/Fundamentals/x936.html

-Bill
Oct 29 '05 #2
> I would like to write a regexp that can be either case sensitive or that can
ignore the case based on a variable (value would be either 'i' or '').
For instance in the below code the variable is $case.
I can't make that piece of code working fine. Anyone can help please ?

my $str = "Hello World!";
my $pattern "hello";
my $case = "i";

if ($str =~ s/${pattern}/${case}) {
print "match!\n";
}


The above code doesn't actually run, but I'm assuming that was just typos
when you posted it. It helps to put the code in a file and test it first.

As for solving your regex question, you might want to take a look at:

perldoc perlre

Read the section entitled "Extended Patterns". You can set regex flags
(such as "i" for "ignore case") in the pattern itself. For example:

my $str = "Hello World!";
my $case = "i";
my $pattern = "(?$case)hello" ;

if ($str =~ /${pattern}/) {
print "match!\n";
}

-- Lars

--
la****@hotmail. com

Nov 22 '05 #3

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

Similar topics

5
2359
by: Lukas Holcik | last post by:
Hi everyone! How can I simply search text for regexps (lets say <a href="(.*?)">(.*?)</a>) and save all URLs(1) and link contents(2) in a dictionary { name : URL}? In a single pass if it could. Or how can I replace the html &entities; in a string "blablabla&amp;blablabal&amp;balbalbal" with the chars they mean using re.sub? I found out they are stored in an dict . I though about this functionality:
7
1738
by: serpent17 | last post by:
Hello all, I have this line of numbers: 04242005 18:20:42-0.000002, 271.1748608, , , repeated several times in a text file and I would like each element to be part of a vector. how do I do this ? I am not very capable in using
3
4324
by: Jesper Stocholm | last post by:
I need to be able to detect URIs in some text and after this replace dem with HTML-anchors, that is http://www.tempuri.org/page.html should be replaced with <a href="http://www.tempuri.org/page.html">http://www.tempuri.org/page.html</a> I have made the following code:
6
3124
by: Nick | last post by:
Hi, How can I check if a number exists by itself in this string by using the RegExp object? --- var mystring = "11,111,01,011"; var match = "1"; var re = new RegExp( match );
1
1129
by: Chris Dunaway | last post by:
I have a string: 555-1234,12345,"Jones, John" I want to split the string based on a comma as a delimiter. Since the name portion has a comma and is delimited with quotation marks, I need to ignore or strip them out. I also want to reverse the last and first names so that "Jones, John" becomes John Jones (no quotation marks or commas, last and first name reversed). I thought, a regular expression should do the trick. So I came with...
13
1843
by: Phat G5 (G3) | last post by:
I am weak when it comes to regexp but hoped someone might know in this case. I am trying to take a url like this : something.lasso?blah=blah&blah2=blah2&sort=hello&blah3=blah3 And remove the &sort=XXX without hurting the rest of the url. The parameter to be replaced would be a parameter passed to a function. Here is what I have so far: function refresh(item) {
3
1216
by: vendredi5h | last post by:
Hello all, I would like to create a Regexp that (very simplified situation) read a web page and retreive some informations in the table rows (<tr>) that include a particular text except if it include too another particular text. Say I've got the following variable: var myhtml = "
9
34835
by: Laphan | last post by:
Hi All I'm using the below to limit the input into a text box to just letters, numbers, hyphens and full stops, but I also need to allow the backspace, delete and arrow keys to come through. How can I do this? <script language="JavaScript" type="text/javascript"> <!-- function onKeyPressBlockNumbers(e) {
3
1626
by: jgarrard | last post by:
Hi, I have an array of strings which are regular expressions in the PERL syntax (ie / / delimeters). I wish to create a RegExp in order to do some useful work, but am stuck for a way of getting these strings into the RegExp object. The RegExp constructor seems to want two parameters - the non / delimited expression, and the global modifiers.
0
9719
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
10618
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...
0
10366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10110
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
9187
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
7649
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...
1
4329
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
2
3850
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
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.