tags:

views:

92

answers:

3

Hi,

I am not much familiar with regular expressions.
I want help for following regular exceptions:
1. String start with alpha word and then followed by any alpha or number. e.g. Abc 20 Jan to 15 Dec
2. String for a decimal number. e.g. 450,122,224.00
3. Also to check if String contain any pattern like 'Page 2 of 20'

Thanks.

A: 

1 I am not sure what you mean here. A word at te start, followed by any number of words and numbers? Try this one:

^[a-zA-Z]+(\s+([a-zA-Z]+|\d+))+

2 Just a decimal number would be

\d+(\.\d+)?

Getting the commas in there:

\d{1,3}(,\d{3})*(\.\d+)?

3 Use

Page \d+ of \d+
Jens
+2  A: 
// 1. String start with alpha word and then followed by
//    any aplha or number. e.g. Abc 20 Jan to 15 Dec

// One or more alpha-characters, followed by a space,
//     followed by some alpha-numeric character, followed by what ever
Pattern p = Pattern.compile("\\p{Alpha}+ \\p{Alnum}.*");
for (String s : new String[] {"Abc 20 Jan to 15 Dec", "hello world", "123 abc"})
    System.out.println(s + " matches: " + p.matcher(s).matches());

// 2. String for a decimal number. e.g. 450,122,224.00
p = Pattern.compile(
        "\\p{Digit}+(\\.\\p{Digit})?|" +  // w/o thousand seps.
        "\\p{Digit}{1,3}(,\\p{Digit}{3})*\\.\\p{Digit}+"); // w/ thousand seps.
for (String s : new String[] { "450", "122", "224.00", "450,122,224.00", "0.0.3" })
    System.out.println(s + " matches: " + p.matcher(s).matches());


// 3. Also to check if String contain any pattern like 'Page 2 of 20'

// "Page" followed by one or more digits, followed by "of"
// followed by one or more digits.
p = Pattern.compile("Page \\p{Digit}+ of \\p{Digit}+");
for (String s : new String[] {"Page 2 of 20", "Page 2 of X"})
    System.out.println(s + " matches: " + p.matcher(s).matches());

Output:

Abc 20 Jan to 15 Dec matches: true
hello world matches: true
123 abc matches: false
450 matches: true
122 matches: true
224.00 matches: true
450,122,224.00 matches: true
0.0.3 matches: false
Page 2 of 20 matches: true
Page 2 of X matches: false
aioobe
Your decimal number matcher doesn't match thousand separators like `450,122,224.00`.
BalusC
Oh, I thought it was multiple cases... I'll update.
aioobe
A: 

1.) /[A-Z][a-z]*(\s([\d]+)|\s([A-Za-z]+))+/

[A-Z][a-z]* being an uppercase word

\s([\d]+) being a number prefixed be a (white)space

\s([A-Za-z]+) being a word prefixed be a (white)space

2.) /(\d{1,3})(,(\d{3}))*(.(\d{2}))/

(\d{1,3}) being a 1-to-3 digit number

(,(\d{3}))* being 0-or-more three-digit numbers prefixed by a comma

(.(\d{2})) being a 2-digit decimal

3.) /Page (\d+) of (\d+)/

(\d+) being one-or-more digits

When writing this (or any regex) I like to use this tool

sigint