Hi,
Im just new to this language and I would like to know what does this groovy regular expression match too. I was not able to figure it by online articles.if some would would give an explanation i would greatly appreciate.

the 3 regular expressions are :

static propertyPattern = ~/@property\s([\w, =]+)\s(\w+)\s(*?)\w/

static methodPattern1 = ~/(-|+)\s(((?:const)?)\s(\w+)\s(<[\w\s,]+>)?\s(*?))\s(\w+)(:\s(((?:const)?)\s(\w+)\s(<[\w\s,]+>)?\s(*?))\s(\w+))?\s/
static methodPattern2 = ~/(\w+):\s
(((?:const)?)\s(\w+)\s(<[\w\s,]+>)?\s(*?))\s(\w+)/
static methodPattern3 = ~/,\s(...)\s$/

thanks
appreicate a reply

hi can anyone give me a like to a java regular expression debugger that will show the matching parts of the string
thanks in advance

I'm not an expert with regex, but have used it. I think that many of the regex engines use similar syntax although there are some small differences. What worked for me was to start with a small regex expression, test it and then keep adding to it until it did everything I needed. I don't know anything about groovy, but I think that the regular java regex may work.

Rather than starting with:

static propertyPattern = ~/@property\s([\w, =]+)\s(\w+)\s(*?)\w/

Start with

static propertyPattern = ~/@property\s([\w, =]+)/

or less if that is not returning any results.

Here is some basic stuff:
The regular expression is surrounded by forward slashes:

~/ your_regular_expression_goes_here /

\s matches a space, a tab, a line break, or a form feed

\w matches word character [A-Za-z0-9_]

? at end matches zero or one of the preceeding element

* at end matches zero or more of the preceeding element

+ at end matches one or more of the preceeding element

$ at end, matches the end of the string

[] : anything inside bracket are "or"...if it matches anything inside the bracket, it matches.

*? matches like *, however match is smallest possible match

(x) matches "x" and remembers the match (capturing)

(?:x) matches "x" but does not remember the match (non-capturing)

x|y matches either x or y

Also check out regex flags (such as multi-line)

Here are some resources:

  • Regular Expressions in Groovy

  • Regular Expressions

  • Patterns with flags

  • Regular Expression Summary - Syntax

    Groups and capturing

    Group number

    Capturing groups are numbered by counting their opening parentheses from left to right. In the expression ((A)(B(C))), for example, there are four such groups:

    1
    ((A)(B(C)))

    2
    (A)

    3
    (B(C))

    4
    (C)

    Group zero always stands for the entire expression.

    Capturing groups are so named because, during a match, each subsequence of the input sequence that matches such a group is saved. The captured subsequence may be used later in the expression, via a back reference, and may also be retrieved from the matcher once the match operation is complete.

There is a regex tool Expresso 3.0 that I used before--I think that it is for .NET though.

You might also try searching for "regex tester java" using your favorite search engine.

If you post some sample data (that you are trying to search), it may be easier to tell you what will match. Also please mention what you are trying to extract from sample data.

I will attempt to decipher this one:

static propertyPattern = ~/@property\s([\w, =]+)\s(\w+)\s(*?)\w/

Someone can correct me if I'm wrong.

It will search for:

"@property" followed by a single space

followed by a word character, a comma, or equals sign (1 or more times)

followed by another single space

followed by 1 or more word characters

followed by a single space

"*?" matches the preceding character 0 or 1 times (I think), but probably shouldn't be in parentheses by itself, if you remove the parentheses, it would match a space 0 or 1 times 

followed by a single word character

You may want to put parentheses around the entire expression to make it a matching group.

static propertyPattern = ~/(@property\s([\w, =]+)\s(\w+)\s*?\w)/

I don't know too much about it, but know that I had trouble getting mine to work without the surrounding parentheses.

Edited 2 Years Ago by cgeier

Hi cgeier, thank you for the nice explanation.
Herewith I have listed the match pattern and the string that matches. Appreciate if you could give me some explanaing on this. what each charater means in the match pattern.

------
static propertyPattern = ~/@property\s([\w, =]+)\s(\w+)\s(*?)\w/

@property\s([\w, =]+)\s(\w+)\s(*?)\w

------

------
static methodPattern1 = ~/(-|+)\s(((?:const)?)\s(\w+)\s(<[\w\s,]+>)?\s(*?))\s(\w+)(:\s(((?:const)?)\s(\w+)\s(<[\w\s,]+>)?\s(*?))\s(\w+))?\s*/

+ (NSAttributedString *)attributedStringWithAttachment:(NSTextAttachment *)attachment

appreciate a reply, thnaks in advance

what does =~ in groovy code mean? is it an negative assignment?
below is the line
def match = m =~ methodPattern1

thanks

I don't really know anything specifically about Groovy--just some general regular expression stuff. Here is a resource that I found for Groovy.

This article has been dead for over six months. Start a new discussion instead.