Solved

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

Posted on 2012-03-27
9
584 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 74

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now