Trying to use regular expressions to convert names to Title Case

I'm trying to change names to their proper case for most common names in North America (esp. the U.S.).
Some examples are in the comments of the included code below.
My problem is that *retName = retName.replaceAll("( [^ ])([^ ]+)", "$1".toUpperCase() + "$2");* does not work as I expect. It seems that the toUpperCase method call does not actually do anything to the identified group.
Everything else works as I expect.
I'm hoping that I do not have to iterate through each character of the string, upshifting the characters that follow spaces.
Any help from you RegEx experts will be appreciated.
{code}
* Converts names in some random case into proper Name Case. This method does not have the
* extra processing that would be necessary to convert street addresses.
* This method does not add or remove punctuation.
* Examples:
* DAN MARINO --> Dan Marino
* old macdonald --> Old Macdonald <-- Can't capitalize the 'D" because of Ernst Mach
* ROY BLOUNT, JR. --> Roy Blount, Jr.
* CAROL mosely-BrAuN --> Carol Mosely-Braun
* Tom Jones --> Tom Jones
* ST.LOUIS --> St. Louis
* ST.LOUIS, MO --> St. Louis, Mo <-- Avoid City Names plus State Codes
* This is a work in progress that will need to be updated as new exceptions are found.
public static String toNameCase(String name) {
* Basic plan:
* 1. Strategically create double spaces in front of characters to be capitalized
* 2. Capitalize characters with preceding spaces
* 3. Remove double spaces.
// Make the string all lower case
String retName = name.trim().toLowerCase();
// Collapse strings of spaces to single spaces
retName = retName.replaceAll("[ ]+", " ");
// "mc" names
retName = retName.replaceAll("( mc)", " $1");
// Ensure there is one space after periods and commas
retName = retName.replaceAll("(\\.|,)([^ ])", "$1 $2");
// Add 2 spaces after periods, commas, hyphens and apostrophes
retName = retName.replaceAll("(\\.|,|-|')", "$1 ");
// Add a double space to the front of the string
retName = " " + retName;
// Upshift each character that is preceded by a space
// For some reason this doesn't work
retName = retName.replaceAll("( [^ ])([^ ]+)", "$1".toUpperCase() + "$2");
// Remove double spaces
retName = retName.replaceAll(" ", "");
return retName;
Edited by: FuzzyBunnyFeet on Jan 17, 2011 10:56 AM
Edited by: FuzzyBunnyFeet on Jan 17, 2011 10:57 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

Hopefully someone will still be able to provide a RegEx solution, but until that time here is a working method.
Also, if people have suggestions of other rules for letter capitalization in names, I am interested in those too.
* Converts names in some random case into proper Name Case.  This method does not have the
* extra processing that would be necessary to convert street addresses.
* This method does not add or remove punctuation.
* Examples:
* CAROL mosely-BrAuN --> Carol Mosely-Braun
* carol o'connor --> Carol O'Connor
* DAN MARINO --> Dan Marino
* eD mCmAHON --> Ed McMahon
* joe amcode --> Joe Amcode         <-- Embedded "mc"
* mr.t --> Mr. T                    <-- Inserted space
* OLD MACDONALD --> Old Macdonald   <-- Can't capitalize the 'D" because of Ernst Mach
* old mac donald --> Old Mac Donald
* ROY BLOUNT,JR. --> Roy Blount, Jr.
* ST.LOUIS --> St. Louis
* ST.LOUIS,MO --> St. Louis, Mo     <-- Avoid City Names plus State Codes
* Tom Jones --> Tom Jones
* This is a work in progress that will need to be updated as new exceptions are found.
public static String toNameCase(String name) {
     * Basic plan:
     * 1.  Strategically create double spaces in front of characters to be capitalized
     * 2.  Capitalize characters with preceding spaces
     * 3.  Remove double spaces.
    // Make the string all lower case
    String workStr = name.trim().toLowerCase();
    // Collapse strings of spaces to single spaces
    workStr = workStr.replaceAll("[ ]+", " ");
    // "mc" names
    workStr = workStr.replaceAll("( mc)", "  $1  ");
    // Ensure there is one space after periods and commas
    workStr = workStr.replaceAll("(\\.|,)([^ ])", "$1 $2");
    // Add 2 spaces after periods, commas, hyphens and apostrophes
    workStr = workStr.replaceAll("(\\.|,|-|')", "$1  ");
    // Add a double space to the front of the string
    workStr = "  " + workStr;
    // Upshift each character that is preceded by a space and remove double spaces
    // Can't upshift using regular expressions and String methods
    // workStr = workStr.replaceAll("( [^ ])([^ ]+)", "$1"toUpperCase() + "$2");
    StringBuilder titleCase = new StringBuilder();
    for (int i = 0; i < workStr.length(); i++) {
        if (workStr.charAt(i) == ' ') {
            if (workStr.charAt(i+1) == ' ') {
                i += 2;
            while (i < workStr.length() && workStr.charAt(i) == ' ') {
                titleCase.append(workStr.charAt(i++));
            if (i < workStr.length()) {
                titleCase.append(workStr.substring(i, i+1).toUpperCase());
        } else {
            titleCase.append(workStr.charAt(i));
    return titleCase.toString();
{code}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

Similar Messages

  • Using regular expressions for validating time fields

    Similar to my problem with converting a big chunk of validation into smaller chunks of functions I am trying to use Regular Expressions to handle the validation of many, many time fields in a flexible working time sheet.
    I have a set of FormCalc scripts to calculate the various values for days, hours and the gain/loss of hours over a four week period. For these scripts to work the time format must be in HH:MM.
    Accessibility guidelines nix any use of message box pop ups so I wanted to get around this by having a hidden/visible field with warning text but can't get it to work.
    So far I have:
    var r = new RegExp(); // Create a new Regular Expression Object
    r.compile ("^[00-99]:\\] + [00-59]");
    var result = r.test(this.rawValue);
    if (result == true){
    true;
    form1.flow.page.parent.part2.part2body.errorMessage.presence = "visible";
    else (result == false){
    false;
    form1.flow.page.parent.part2.part2body.errorMessage.presence = "hidden";
    Any help would be appreciated!

    Date and time fields are tricky because you have to consider the formattedValue versus the rawValue. If I am going to use regular expressions to do validation I find it easier to make them text fields and ignore the time patterns (formattedValue). Something like this works (as far as my very brief testing goes) for 24 hour time where time format is HH:MM.
    // form1.page1.subform1.time_::exit - (JavaScript, client)
    var error = false;
    form1.page1.subform1.errorMsg.rawValue = "";
    if (!(this.isNull)) {
      var time_ = this.rawValue;
      if (time_.length != 5) {
        error = true;
      else {
        var regExp = /^([01]?[0-9]|2[0-3]):[0-5][0-9]$/;
        if (!(regExp.test(time_))) {
          error = true;
    if (error == true) {
      form1.page1.subform1.errorMsg.rawValue = "The time must be in the format HH:MM where HH is 00-23 and MM is 00-59.";
      form1.page1.subform1.errorMsg.presence = "visible";
    Steve

  • Syntax error for using Regular Expression

    HI all,
    I tried to use Regular expresion to convert my string
    my sample code is like this:
    REPLACE ALL OCCURRENCES OF REGEX '\w*' IN INTAB-SCITY WITH '$1_$2'.
    REPLACE ALL OCCURRENCES OF REGEX '\w*' IN INTAB-DCITY WITH '$1_$2'.
    REPLACE ALL OCCURRENCES OF REGEX '\w*' IN INTAB-BCITY WITH '$1_$2'.
    While activating, I got this syntax error:
    The word REGEX is reserved
    What happen?  Did I got a old ECC version?  Or something else got wrong??
    Any help would be appreciated.
    Edited by: Chih-Chieh Chan on Dec 10, 2008 8:58 AM

    My ECC version is 5.0
    does it make some difference?

  • Help in query using regular expression

    HI,
    I need a help to get the below output using regular expression query. Please help me.
    SELECT REGEXP_SUBSTR ('PWRPKG(P/W+P/L+CC)', '[^+]+', 1, lvl) val, lvl
    FROM DUAL,(SELECT LEVEL lvl FROM DUAL
    CONNECT BY LEVEL <=(SELECT MAX ( LENGTH ('PWRPKG(P/W+P/L+CC)') - LENGTH (REPLACE ('PWRPKG(P/W+P/L+CC)','+',NULL))+ 1) FROM DUAL));
    I need the output as
    correct result:
    ==============
    val lvl
    P/W 1
    P/L 2
    CC 3
    But i tried the above it is not coming the above result. Please help me where i did a mistake.
    Thanks in advance

    Frank gave you a solution in your other thread. You could simplify it if you are on 11g:
    SQL> select * from table_x
      2  /
    TXT
    TECHPKG(INTELLI CC+FRT SONAR)
    PWRPKG(P/W+P/L+CC)
    select  txt,
            regexp_substr(
                          txt,
                          '(.*\()*([^+)]+)',
                          1,
                          column_value,
                          null,
                          2
                         ) element,
            column_value element_number
      from  table_x,
            table(
                  cast(
                       multiset(
                                select  level
                                  from  dual
                                  connect by level <= regexp_count(txt,'\+') + 1
                       as sys.OdciNumberList
      order by rowid,
               column_value
    TXT                                      ELEMENT    ELEMENT_NUMBER
    TECHPKG(INTELLI CC+FRT SONAR)            INTELLI CC              1
    TECHPKG(INTELLI CC+FRT SONAR)            FRT SONAR               2
    PWRPKG(P/W+P/L+CC)                       P/W                     1
    PWRPKG(P/W+P/L+CC)                       P/L                     2
    PWRPKG(P/W+P/L+CC)                       CC                      3
    SQL>  SY.

  • Changeparticular characters in a string by using regular expressions ...

    Hello Everyone,
    I am trying to write a function by using oracles regular expression function REGEXP_REPLACE but I could not succed till now.
    My problem as follows, I have a text in a column for example let say 'sdfsdf Sdfdfs Sdfd' I want replace all s and S characters with X and make the text look like 'XdfXdf XdfdfX Xdfd'.
    Is it possible by using regular expressions in oracle ?
    Can you give me some clues ?
    Thank you

    SSU wrote:
    Hello Everyone,
    I am trying to write a function by using oracles regular expression function REGEXP_REPLACE but I could not succed till now.
    My problem as follows, I have a text in a column for example let say 'sdfsdf Sdfdfs Sdfd' I want replace all s and S characters with X and make the text look like 'XdfXdf XdfdfX Xdfd'.
    Is it possible by using regular expressions in oracle ?
    Can you give me some clues ?
    Thank you
    SQL> SELECT
      2  regexp_replace('sdfsdf Sdfdfs Sdfd','s|S','X') from dual;
    REGEXP_REPLACE('SD
    XdfXdf XdfdfX XdfdRegards,
    Achyut

  • Using Regular Expressions To Remove Characters JDK 1.4

    I want to write a regular expression to remove all commas in a string of text.
    string is:
    1,000
    or 1,000,000
    I want it to return 1000 and 1000000.
    I have tried some but I am just starting with Regular Expressions.
    Please Help!

    Try this tutorial: Linux : Education : Tutorials
    Using regular expressions
    David Mertz
    President, Gnosis Software, Inc.
    September 2000
    http://www-105.ibm.com/developerworks/education.nsf/linux-onlinecourse-bytitle/6C2B4863702F592B8625696200589C5B?OpenDocument

  • Using Regular Expressions to replace Quotes in Strings

    I am writing a program that generates Java files and there are Strings that are used that contain Quotes. I want to use regular expressions to replace " with \" when it is written to the file. The code I was trying to use was:
    String temp = "\"Hello\" i am a \"variable\"";
    temp = temp.replaceAll("\"","\\\\\"");
    however, this does not work and when i print out the code to the file the resulting code appears as:
    String someVar = ""Hello" i am a "variable"";
    and not as:
    String someVar = "\"Hello\" i am a \"variable\"";
    I am assumming my regular expression is wrong. If it is, could someone explain to me how to fix it so that it will work?
    Thanks in advance.

    Thanks, appearently I'm just doing something weird that I just need to look at a little bit harder.

  • Using regular expressions to get a customized output

    Hi,
    I have a string/varchar variable with the data ',a,b,c,' in it.
    I want the display as follows:
    a
    b
    c
    I would like to get the similar output using regular expressions.
    How do I get this output using REGEXP_REPLACE or REGEXP_SUBSTR?
    Please do the needful.
    Thanks & Regards,
    Rakshit

    I remember that, however if we look closer, that one has a little flaw: The 2nd row should be null, because ",," indicates an empy field. The MODEL clause solution works just fine in this case:
    with t as (select 'aaaa,,bbbb,cccc,dddd,eeee,ffff' col1 from dual)
    -- end of sample data
    SELECT col_new
      FROM t
    MODEL
       PARTITION BY (ROWNUM rn)
       DIMENSION BY (0 dim)
       MEASURES(col1, col1 col_new)
       RULES ITERATE(99) UNTIL (ITERATION_NUMBER = LENGTH(REGEXP_REPLACE(col1[0], '[^,]')))
                    (col_new[ITERATION_NUMBER] = REPLACE(REGEXP_SUBSTR(col1[0], '(^|,)[^,]*', 1, ITERATION_NUMBER+1), ','))
    COL_NEW                                                                                                                                                                  
    aaaa                                                                                                                                                                     
    bbbb                                                                                                                                                                     
    cccc                                                                                                                                                                     
    dddd                                                                                                                                                                     
    eeee
    ffff
    7 Zeilen ausgewählt.Update: I had this nagging feeling that I missed something, and there it was. If you want to see what the problem with my solution is, change the example to
    with t as (select ',aaaa,,bbbb,cccc,dddd,eeee,ffff' col1 from dual)So I went back and tried to fix BlueShadows approach. Here it is:
    with t as (select 'aaaa,,bbbb,cccc,dddd,eeee,ffff' txt from dual)
    -- end of sample data
    SELECT REPLACE(REGEXP_SUBSTR(',' || txt, ',[^,]*', 1, level), ',') col_new
      FROM t
      CONNECT BY level <= length(regexp_replace(txt,'[^,]*'))+1
    ;C.

  • How to fetch substring using regular expression

    Hi,
    I am new to using regular expression and would like to know some basic details of how to use them in Java.
    I have a String example= "http://www.google.com/foobar.html#*q*=database&aq=f&aqi=g10&fp=c9fe100d9e542c1e" and would like to get the value of "q" parameter (in bold) using regular expression in java.
    For the same example, when we tried using javascript:
    match = example.match("/^http:\/\/(?:(?!mail\.)[^\.]+?\.)?google\.[^\?#]+(?:.*[\?#&](?:as_q|q)=([^&]+))?/i}");
    document.write('
    ' + match);
    We are getting the output as: http://www.google.com/foobar.html#q=database,*database* where the bold text is the value of "q" parameter.
    In Java we are trying to get the value of the q parameter separately or atleast resembles the output given by JavaScript. Please help me resolving this issue.
    Regards
    Praveen

    BalusC wrote:
    Regex is a cumbersome solution for fixed patterns like URL's. String#substring() in combination with String#indexOf would most likely already suffice.I usually agree, although, in this case, finding the exact parameter might be difficult without a small regex, perhaps:
    "\\wq=\\s*"in conjunction with Pattern/Matcher, used similarly to an indexOf() to find the start of the parameter value.
    Winston

  • ADF Email Validation using Regular Expression

    Hi,
    Wanted to add Email Validation VO search.
    It is working if i put
    <af:validateRegExp pattern="[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}"
                                             messageDetailNoMatch="The value {1} is not a valid email address:"/>However this requires email id to be entered in Capital Letters.
    Tried with below option is not working.
                   <af:inputText value="#{bindings.xxEmail.inputValue}" label="Email"
                                          required="#{bindings.xxEmail.hints.mandatory}"
                                          columns="#{bindings.xxEmail.hints.displayWidth}"
                                          maximumLength="#{bindings.xxEmail.hints.precision}"
                                          shortDesc="#{bindings.xxEmail.hints.tooltip}" id="it5">
                                <f:validator binding="#{bindings.xxEmail.validator}"/>
                                <f:validateLength minimum="6"/>             
                                <af:validateRegExp pattern="[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}"
                                             messageDetailNoMatch="The value {1} is not a valid email address:"/>
                            </af:inputText>I got above info from
    ADF Email Validation using Regular Expression
    User don't enter email id Without @ .
    Kindly suggest pattern to achive this.
    Thanks,
    jit
    Edited by: appsjit on Jan 25, 2013 7:08 PM

    The RegEx to check EMail after RFC2822
    [a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?There are still some missing parts in the check as not all suffix combinations are allowed, but this is pretty good.
    Timo

  • Validate cfinput using regular expression

    Hi,
    can somebody help me valditing an cfinput field using regular expressions?
    First digit must be a number or "R".
    Digit 2-15 can be everything without special characters. Digit 2-15 can also be empty.
    I try this, but it doesn't work (Sorry I'm a beginner using regex).
    <cfinput type="text" name="field1" required="yes" validate="regular_expression"  pattern="[0-9Rr][0-9a-zA-Z]*"  maxlength="15">
    Thank you in advice!
    Claudia

    You haven't told your regex to match the entire string, so it will "pass"
    any string that has your match anywhere within it, so like as long as it's
    got an R or a digit in it: it's OK.
    To tell it to match the entire string, anchor it to the start and end of the
    string with ^ and $ respectively.
    Regular expression syntax: Using special characters
    http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec0a38f -7ffb.html#WSc3ff6d0ea77859461172e0811cbec0a38f-7fef
    Adam

  • Procedure using regular expression

    How to write a procedure using regular expression where i pass a string as input
    The procedure should check whether it is a valid email address or not
    Please help me

    Hello,
    perhaps you don't need to code it, because it's already there.
    When you use the database to send your mails it or the appropriate package throws the exception
    ORA-29279: Permanenter SMTP-Fehler: 501 5.5.4 Invalid Address
    When you just need a procedure to check it you can write a wrapper for a java function.
    import javax.mail.internet.*;
    import oracle.sql.NUMBER;
    public class mail_utility {
      public static NUMBER validate_address(String rfc822Address) {
        int rc = 0;
        try {
          InternetAddress ia = new InternetAddress(rfc822Address);
          rc = 1;
        } catch (AddressException ae) {
          rc = 0;
        } catch (Exception e) {
          rc = -1;
        } finally {
          return new NUMBER(rc);
    CREATE OR REPLACE  FUNCTION VALIDATE_ADDRESS (p_address in varchar2)
    return number
    as language java name
      'mail_utility.validate_address(java.lang.String) return oracle.sql.NUMBER';I think i've got it from the forum but i don't remember from whom.
    Bernd

  • How to use Regular expression

    Hi,
    I have a file that contains this format (separated by ;(semicolon) ):
    user id;user name;email address;password;integer;list of integer(separated by ,(comma))
    below is the example data :
    abc;Abc;[email protected];password1;1;1,2
    def;Def;[email protected];password;2;1,2,3
    ghi;Ghi;[email protected];password;2;1
    my question is how to verify the valid input for each row using regular expression..? TQ

    @Op. Doing a correct validation of e-mailaddresses
    is very hard using regular expressions (doingbasic
    validation is however easy)
    http://www.regular-expressions.info/email.html
    I like the RFC 822 compliant regexp :)

  • How to use regular expressions to generate test data ?

    Hi
    Someone can help me on what I have to do in order to create test data with regular expressions ?
    For example, I want to introduce a random telephone number (XXX-XXXX) in the phone number Form Field, I want to create the phone number using regular expressions in order to test different values in each playback of the script.
    I don't want to use VB or vbscript in e-tester, I'm just trying to do this with e-load nav editor and e-load
    Thanks a lot

    Hi and thanks for your answer!, it's a great trick ^_^
    I'm doing a research on how to improve the execution speed of the scripts in e-load, so actually I'm trying to avoid the use of databanks and VB code also.
    I was expecting that maybe e-load, e-load nav editor or e-tester can automatically generate test data via Regular Expressions. Someone Knows if this is possible ?
    Also can anyone tell me what the option "Automatically Generated (complex)" means ? I think that this will help me a lot
    *you can find this option in e-load Nav Editor when you select a parameter in the tree view, then go to the  "type" listbox in the properties pane, there you will find this option and some more options like :"Databanked variable", "Custom Dynamic Value", "Function".. etc.
    Thanks again

  • I am trying to use macro express and adobe acrobat 9 profession keeps shutting down, why is this happening?  I tried to run as administrator already as well on both Macro Express and in Adobe and it still keeps shutting down.

    I am trying to use macro express and adobe acrobat 9 profession keeps shutting down, why is this happening?  I tried to run as administrator already as well on both Macro Express and in Adobe and it still keeps shutting down.

    same problem, it's been happening to me for a week or two now i'm thinking about backing up my documents and just wiping it completely, see if that works. Has anyone else tried this? I'm loosing time and have already lost a good few hours of work as it just crashes randomly. need help!!!
    - saving these threads on my favourites cause i'm about to crash . . .

Maybe you are looking for

  • IPv6 ACLs for ZBFW with changing IPv6 prefix?

    Hi all Is there a trick to keep IPv6 ACLs for ZBFW working when the IPv6 prefix will change ? Background: 6RD based residential internet access. Provider has a /28 6RD-Prefix, and will append the whole 32bits of the DHCP assigned public IPv4 address,

  • Accurate RoboHelp to PDF conversion?

    I've been attempting to generate PDFs that accurately reflect my RoboHelp projects. The output always comes out a bit funky (blank pages, orphaned headings and text, etc.). Also, the PDFs generate with headers which I don't want and the TOC as the fr

  • WCS not graphing CleanAir Interferers

    I have WCS managing 4 controllers.  1 of which has clean air enabled.  WCS gives air quality reports just fine, but it gives me no information on the Device Interferers or the Interferer count.  I can go to the controller, go to monitor>cleanair and

  • LOGIC 8 Crashing after 16 1/2 measures

    Hears the shortened error log. Any ideas? It never fails to crash at that length wheter lopped, soloed on a given midi track or not. Originally the error message said rewire was the issue but I am up to date and I've tried removing it and still no lu

  • Network Diagrams

    HI, Network diagrams are drawn according to physical or logical connectivity?