将数字替换为java中正确位置的单词

时间:2016-06-27 11:19:59

标签: java

实际上,我试图将数字替换为用户提供的句子中的单词。本案例日期格式;例如:My birthday is on 16/6/2000 and I'm newbie to the java - > 成为 ---> My birthday is on sixteenth july two thousand and I'm newbie to the java

这是代码:

        Scanner reader = new Scanner(System.in);
        System.out.println("Enter any numbers: ");
        String nom = reader.nextLine(); // get input from user

        //checking contains that has "/" or not
        if(nom.contains("/")){
           String parts[] = nom.split("[/]"); 
           String part1 = parts[0]; //data before "/" will be stored in the first array
           String day[] = part1.split("\\s+");// split between space
           String get_day = day[day.length -1];// get last array

           String get_month = parts[1]; //data in the between of "/" will be stored in the second array

           String part3 = parts[2]; // data after "/" will be stored in the third array
           String year[] = part3.split("\\s+");// split between space
           String get_year = year[0];// get first array

           String s = NumberConvert.convert(Integer.parseInt(get_day)) + 
                      NumberConvert.convert(Integer.parseInt(get_month)) + 
                      NumberConvert.convert(Integer.parseInt(get_year));

           String con = nom.replaceAll("[0-9].*/[0-9].*/[0-].*", s); // replace number to word        
           System.out.println(con); // print the data already converted
        } else {....}

但我得到的结果是:

My birthday is on sixteenth july two thousand 

//"and I'm newbie to the java" is disappear [How to solve it]//

如何解决它。实际上我想在" /"之前和之后获得价值。斜杠并将其转换为单词并将其替换为用户的原始输入。

我试过的是:

String con = nom.replaceAll("[0-9].*/[0-9].*/[0-9999]", s); // a bit change [0-9].* to [0-9999] 

但是输出变成这样:

My birthday is on sixteenth july two thousand 000 and I'm newbie to the java
//after two thousand index "000" is appearing

2 个答案:

答案 0 :(得分:3)

正则表达式错误:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>EusurveyAdminB</groupId>
    <artifactId>EusurveyAdminB</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>

        </plugins>


    </build>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>3.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>3.2.6.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>3.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.3</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>antlr</groupId>
            <artifactId>antlr</artifactId>
            <version>2.7.7</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-c3p0</artifactId>
            <version>4.1.3.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.1.3.Final</version>
            <type>jar</type>
            <!-- <scope>compile</scope> -->
        </dependency>



        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0</version>
        </dependency>



        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.15</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.mail</groupId>
                    <artifactId>mail</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>javax.jms</groupId>
                    <artifactId>jms</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jdmk</groupId>
                    <artifactId>jmxtools</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jmx</groupId>
                    <artifactId>jmxri</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.1.3.Final</version>
            <type>jar</type>
            <!-- <scope>compile</scope> -->
            <exclusions>
                <exclusion>
                    <artifactId>xml-apis</artifactId>
                    <groupId>xml-apis</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-c3p0</artifactId>
            <version>4.1.3.Final</version>
        </dependency>

        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0</version>
        </dependency>

    </dependencies>


</project>

含义:

[0-9].*/[0-9].*/[0-].*

应该是:

[0-9] match a single number in the range between 0 and 9
.* matches any character (except newline) between zero and unlimited times, as many times as possible, giving back as needed [greedy]
/ matches the character / literally
[0-9] match a single number in the range between 0 and 9
.* matches any character (except newline) between zero and unlimited times, as many times as possible, giving back as needed [greedy]
/ matches the character / literally
[0-] match a single number in the list 0- literally
.* matches any character (except newline) between zero and unlimited times, as many times as possible, giving back as needed [greedy]

或者,更好:

[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]

答案 1 :(得分:0)

您还可以使用以下正则表达式模式从String获取所有数字:

        String st = "My birthday is on 16/6/2000 and I'm newbie to the java, using since 2015";     
        Pattern p = Pattern.compile("-?\\d+");
        Matcher m = p.matcher(st);
        while (m.find()) {
          System.out.println(m.group());
        }