What are good regular expressions?

后端 未结 9 915
我寻月下人不归
我寻月下人不归 2021-02-08 05:00

I have worked for 5 years mainly in java desktop applications accessing Oracle databases and I have never used regular expressions. Now I enter Stack Overflow and I see a lot of

相关标签:
9条回答
  • 2021-02-08 05:04

    As you may know, Oracle now has regular expressions: http://www.oracle.com/technology/oramag/webcolumns/2003/techarticles/rischert_regexp_pt1.html. I have used the new functionality in a few queries, but it hasn't been as useful as in other contexts. The reason, I believe, is that regular expressions are best suited for finding structured data buried within unstructured data.

    For instance, I might use a regex to find Oracle messages that are stuffed in log file. It isn't possible to know where the messages are--only what they look like. So a regex is the best solution to that problem. When you work with a relational database, the data is usually pre-structured, so a regex doesn't shine in that context.

    0 讨论(0)
  • 2021-02-08 05:05

    A regular expression (regex or regexp for short) is a special text string for describing a search pattern. You can think of regular expressions as wildcards on steroids. You are probably familiar with wildcard notations such as *.txt to find all text files in a file manager. The regex equivalent is .*\.txt$.

    A great resource for regular expressions: http://www.regular-expressions.info

    0 讨论(0)
  • 2021-02-08 05:13

    These RE's are specific to Visual Studio and C++ but I've found them helpful at times:

    Find all occurrences of "routineName" with non-default params passed:

    routineName\(:a+\)

    Conversely to find all occurrences of "routineName" with only defaults: routineName\(\)

    To find code enabled (or disabled) in a debug build:

    \#if._DEBUG*

    Note that this will catch all the variants: ifdef, if defined, ifndef, if !defined

    0 讨论(0)
  • 2021-02-08 05:16

    If you're just starting out with regular expressions, I heartily recommend a tool like The Regex Coach:

    http://www.weitz.de/regex-coach/

    also heard good things about RegexBuddy:

    http://www.regexbuddy.com/

    0 讨论(0)
  • 2021-02-08 05:18

    Regular Expressions (or Regex) are used to pattern match in strings. You can thus pull out all email addresses from a piece of text because it follows a specific pattern.

    In some cases regular expressions are enclosed in forward-slashes and after the second slash are placed options such as case-insensitivity. Here's a good one :)

    /(bb|[^b]{2})/i
    

    Spoken it can read "2 be or not 2 be".

    The first part are the (brackets), they are split by the pipe | character which equates to an or statement so (a|b) matches "a" or "b". The first half of the piped area matches "bb". The second half's name I don't know but it's the square brackets, they match anything that is not "b", that's why there is a roof symbol thingie (technical term) there. The squiggly brackets match a count of the things before them, in this case two characters that are not "b".

    After the second / is an "i" which makes it case insensitive. Use of the start and end slashes is environment specific, sometimes you do and sometimes you do not.

    Two links that I think you will find handy for this are

    1. regular-expressions.info
    2. Wikipedia - Regular expression
    0 讨论(0)
  • 2021-02-08 05:22

    Consider an example in Ruby:

    puts "Matched!" unless /\d{3}-\d{4}/.match("555-1234").nil?
    puts "Didn't match!" if /\d{3}-\d{4}/.match("Not phone number").nil?
    

    The "/\d{3}-\d{4}/" is the regular expression, and as you can see it is a VERY concise way of finding a match in a string.

    Furthermore, using groups you can extract information, as such:

    match = /([^@]*)@(.*)/.match("myaddress@domain.com")
    name = match[1]
    domain = match[2]
    

    Here, the parenthesis in the regular expression mark a capturing group, so you can see exactly WHAT the data is that you matched, so you can do further processing.

    This is just the tip of the iceberg... there are many many different things you can do in a regular expression that makes processing text REALLY easy.

    0 讨论(0)
提交回复
热议问题