Solved

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

Posted on 2012-03-27
9
599 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Writing a parser for java language 4 83
what are these modules in perl script 4 103
REXEX help Part 2 2 39
CC Dreamweaver Regular Expression Problem 14 18
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…

838 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