1

I'm trying to come up with a pattern to match a string containing "::" 0 or more times but not to match any strings with a single ":" by itself.

So for instance if the group I'm trying to match is arg in "caller message: arg" and arg is something like Class::method() then it should match. I don't want it to find a single ":" in case the string is "caller message1: arg1 message2: arg2" in which case I'd like the pattern to match both arg1 and arg2 but not confuse arg1 message: arg2 as the string to match.

So I have something like [^\[\]\s:]+[:{2}]+[^\[\]\s:]+|[^\[\]\s:]+ to match the arg section (which is trying to find 0 or 1 occurrence of "::"). Clearly I'm no regex expert (or regexpert (or repert) ;) ) and I'm curious to know what the generalised version of this might look like. That is, what regex would I need to search for a substring that doesn't appear in any partial form in the pattern. So if I'm looking for "string", then "s", "st", "str" or any substring of "string" that starts at the beginning (a more generalised version could disallow any substring) cannot appear in the match.

The problem with the idea in the above regex is that it doesn't take into account multiple instances of "::" for example in "Class1::method1(Class2::method2())".

I'm looking for an answer specifically for the whole ":" issue but feel free to answer in a generalised form as well.

EDIT:

tl;dr: I want to match Class1::method1(Class2::method2()) in caller message: Class1::method1(Class2::method2()) or match that in addition to Class3::method3(Class4::method4()) in caller message1: Class1::method1(Class2::method2()) message2: Class3::method3(Class4::message4()) (separately) but the pattern mustn't match the Class1::method1(Class2::method2()) message2: Class3::method3(Class4::method4())

EDIT2:

To clarify: pattern must match in "A B: C::D()" the string C::D() and in A B: C::D() E: F::G()" the strings C::D() and F::G() but not C::D() E: F::G().

I don't think I can be clearer about the intention and why a simple (find me anything)::(anything again) is insufficient.

EDIT3:

Also, there could technically be a space in the part after each : like C::D("some text") which means the regex I mentioned earlier is incorrect and should be more like [^\[\]:]+[:{2}]+[^\[\]:]+|[^\[\]:]+

Here's a real world (modified) example I'm struggling with:

[layer runAction: Sequence::actions(actionScale, NULL)] which need to match layer, runAction and Sequence::actions(actionScale, NULL) in the complete pattern.

And this:

[Sequence actions: DelayTime::create(1.0), CallFunc::create(this, selector(explodeCells)), NULL] which must match Sequence, actions and DelayTime::create(1.0)..., NULL

And this:

[Analytics reportEvent: GENERAL_EVENT usingID: getCurrentID() withParameter: Number::numberWithFloat(Number::defaultNumber() + 1)] must match Analytics, reportEvent, GENERAL_EVENT, usingID (can be ignored but for simplicity it can be matched as well), getCurrentID(), withParameters (can also be ignored), Number::numberWithFloat(Number::defaultNumber() + 1).

EDIT4:

I changed the final example in EDIT3 to reflect the final objective I'm trying to achieve. Here's a DEMO.

6
  • @anubhava I'm playing around with regex101.com and specifically I'm trying to get it to work using python.re Commented Nov 12, 2015 at 12:56
  • You question is rather verbose and confusing. Can you paste an input and output with what you expect? Commented Nov 12, 2015 at 12:57
  • Add a few sample cases, the matches you want to perform etc. as well Commented Nov 12, 2015 at 12:58
  • .................. whut? Commented Nov 12, 2015 at 12:59
  • Your examples are still not clear. Commented Nov 12, 2015 at 13:10

3 Answers 3

2

I'm not exactly sure what you're looking for but here is what I came up with: DEMO

(\S+::\S+\))
Sign up to request clarification or add additional context in comments.

Comments

1

If your examples are accurate, the easiest way would be to use the post-fix to identify the argument.

See this regex101

The regex is simply

message\d*:\s*(\S+)

Dunno if this helps you at all, but...

Regards

EDIT:

OK - here we go again :) This should do it for you...

\w+:\s*(.*?)(?=$|]|\s*\w+:(?:\w|\s))

regex101 example

If not - your examples and instructions aren't clear enough (for me at least ;)

Explanation: It matches word characters followed by a colon and grabs everything after it up to either 1. end of line, 2. a closing bracket or 3. a new sequence of word characters followed by one colon.

2 Comments

Lol that's cute :) I'm not looking for message\d. I think \w is sufficient though. The problem is also that there could be a space in what appears after the : like A::B("some text"). I'll add that point to the question.
The EDIT looks good. I'll plug it in and test and mark this as the answer if it works :)
-1

If you're trying to match a string that can ONLY contain :: but not : try this:

([^:]|::)*

The generalized version is very simple. Every prefix starts with the first character, so that's not allowed unless it's a total match:

([^s]|string)*

3 Comments

Yes but that would accept anything before and after the :: including a single : which is not what I want.
@ClasG And that is an issue why? It contains :: but no single :.
@itchy23 Updated my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.