Solved

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

Posted on 2012-03-27
9
617 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

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.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Six Sigma Control Plans

696 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