Solved

How to use Regex to match anything but a period and a comma

Posted on 2012-03-27
9
594 Views
Last Modified: 2012-03-27
I'm trying to do a match on things such as thomas. - any string ends up with a period or a comma.  How should I do it?  Thanks.
0
Comment
Question by:thomaszhwang
9 Comments
 
LVL 4

Accepted Solution

by:
MichaelStaszewski earned 143 total points
ID: 37773172
Try this.

[^.,]+

Open in new window

0
 

Author Comment

by:thomaszhwang
ID: 37773185
Shouldn't I use the escape? I tried the following, but it doesn't work...

[^\.\,]+

Open in new window

0
 
LVL 10

Assisted Solution

by:pfrancois
pfrancois earned 72 total points
ID: 37773204
Obviously: in regex: don't escape the dot inside of brackets [ ]. Never escape a comma.
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 31

Assisted Solution

by:farzanj
farzanj earned 72 total points
ID: 37773226
If you want to match lines that end with a comma or period use

[,.]$

Within [ ], you don't need to escape except for \[ and \]
0
 
LVL 4

Assisted Solution

by:MichaelStaszewski
MichaelStaszewski earned 143 total points
ID: 37773232
Shouldn't I use the escape? I tried the following, but it doesn't work...

No, that is a character class. Did you try what I wrote exactly? What I wrote should do it. You can review character classes (and many other useful items) here.

http://www.regular-expressions.info/reference.html
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 71 total points
ID: 37773400
Obviously: in regex: don't escape the dot inside of brackets [ ]. Never escape a comma.
While it doesn't hurt anything (typically) to do so, you don't have to escape those particular characters within a character class.

Within [ ], you don't need to escape except for \[ and \]
...and the hyphen, if it does not occur as the first or last character of the class   ; )
0
 
LVL 7

Assisted Solution

by:whosbetterthanme
whosbetterthanme earned 71 total points
ID: 37773537
Another way might be:

my $s = 'Thomas.';

if ($s =~ m/(\.$)|(,$)/g) {
print "Found\n";
}
else {
print "Nope\n";
}
0
 
LVL 35

Assisted Solution

by:Terry Woods
Terry Woods earned 71 total points
ID: 37773550
By "string" do you mean a single word, or an entire sentence, or a string?

Assuming you require a period or comma after the value you want, these might work for you:

If you want a single word:
\w+(?=[,.])

If you want a sentence:
[^.,?!]+(?=[,.])

If you want the entire string before the period or comma:
[^,]+(?=[,.])
0
 

Author Closing Comment

by:thomaszhwang
ID: 37774719
Thanks.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question