Complex regex

I would like to use regex in my java program to recognize some functions of my lines. I have this type of string:

`-Author- wrote (-hh -: - mm -)

So, for example, I have a line with:

Cecco wrote (15:12)

and I have to extract the author fields, hh and mm. Obviously I have some kind of limitation:

hh and mm must be numbers

author hasn't any restrictions

I've to consider space between "has wrote" and (

I don’t know how I can use regex, could you help me?

EDIT: I add my fragment:

            String mRegex = "(\\s)+ has wrote \\((\\d\\d):(\\d\\d)\\)";
            Pattern mPattern = Pattern.compile(mRegex);

            String[] str = {
                "Cecco CQ has wrote (14:55)", //OK (matched)
                "yesterday you has wrote that I'm crazy", //NO (different text)
                "Simon has wrote (yesterday)", // NO (yesterday isn't numbers)
                "John has wrote (22:32)", //OK
                "James has wrote(22:11)", //NO (missed space between has wrote and ()
                "Tommy has wrote (xx:ss)" //NO (xx and ss aren't numbers)
            };

            for(String s : str) {
                Matcher mMatcher = mPattern.matcher(s);
                while (mMatcher.find()) {
                    System.out.println(mMatcher.group());
                }
            }
+3
source share
3 answers

homework?

Sort of:

(.+) has wrote \((\d\d):(\d\d)\)

Gotta do the trick

  • () - mark the groups to capture (in the above example, three)
  • .+ - any characters (you did not specify any restrictions)
  • \d - any number
  • \(\) removes parsers as literals instead of capture groups.

using:

Pattern p = Pattern.compile("(.+) has wrote \\((\\d\\d):(\\d\\d)\\)");

Matcher m = p.matcher("Gareth has wrote (12:00)");

if( m.matches()){
    System.out.println(m.group(1));
    System.out.println(m.group(2));
    System.out.println(m.group(3));
}

(HH: mm) , reudx voodoo:

Pattern p = Pattern.compile("(.+) has wrote\\s?(?:\\((\\d\\d):(\\d\\d)\\))?");

Matcher m = p.matcher("Gareth has wrote (12:00)");

if( m.matches()){
    System.out.println(m.group(1));
    System.out.println(m.group(2));
    System.out.println(m.group(3));
}

m = p.matcher("Gareth has wrote");
if( m.matches()){       
    System.out.println(m.group(1));
    // m.group(2) == null since it didn't match anything
}

:

(.+) has wrote\s?(?:\((\d\d):(\d\d)\))?
  • \s? ( , (HH: mm)
  • (?: ... ) - , .. ? ,

, @codinghorror - regex

+2

- .
eclipse http://www.brosinski.com/regex/

, :

([a-zA-Z]*) has wrote \((\d\d):(\d\d)\)
Cecco has wrote (15:12)

Found 1 match(es):

start=0, end=23
Group(0) = Cecco has wrote (15:12)
Group(1) = Cecco
Group(2) = 15
Group(3) = 12

http://www.regular-expressions.info/tutorial.html

+1

, , , Matcher , , (), Matcher.group(int). , , :

: 22:

":(\\d+):" , :

Matcher.group(1)

int. 1. group (0) - , Matcher.group(0) : 22:

, ,

  • "[A-Za-z]"for characters of the alphabet (perhaps you can also safely use "\\w"one that matches the characters of the alphabet, as well as numbers and _).
  • "\\d" for numbers (1,2,3 ...)
  • "+" to indicate that you need one or more previous characters or groups.
0
source

Source: https://habr.com/ru/post/1786630/


All Articles