• Post Reply Bookmark Topic Watch Topic
  • New Topic

little regex help  RSS feed

 
Karsten Daemen
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys!

I have a String wich may contain the character ':' but only if it's followed with whitespace.

I have already the following sollution to the problem:



But this is ugly, is there a way to do this in one regex?

Thanks in advance for your help!
 
Costi Ciudatu
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
split(":\\S") should do the job.

Note:
Your code is not compilable since you try to return a boolean from inside a void method.
Besides, that method (even if it was compilable) does the exact opposite of what you suggested: it rejects the columns that are followed by whitespace.
 
Karsten Daemen
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Costi Ciudatu wrote:split(":\\s") should do the job.

Note:
Your code is not compilable since you try to return a boolean from inside a void method.
Besides, that method (even if it was compilable) does the exact opposite of what you suggested: it rejects the columns that are followed by whitespace.


My bad, I did some bad very bad copy/pasting from my editor, i'll adjust it!

Whit you say would indeed simplify my code, but do you know how I can write this in a regex?

Thanks anyway.
 
Costi Ciudatu
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, above I just gave an example of a regex (that does not help you at all actually):

Here's an implementation that should do the job (it's just a first attempt, if I find something more elegant, I'll post it):


What exactly do you mean by "use in a regex" ?

Note:
Your code is still not correct since it accepts a string that has at least one column followed by whitespace, and not all columns followed by whitespace.
 
Jan Cumps
Bartender
Posts: 2632
17
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Finds all occurrences of : that are not followed by a blank.

(:$) a colon at the end of the string is not allowed
(:\S) a colon with a nonblank behind it is not allowed (but that includes tabs and newlines)
(:\n) a colon with a newline is not allowed
(:\t) a colon with a tab is not allowed.

If this regex returns at least one match, then there is a colon with a nonblank thereafter

Regex tester: http://myregexp.com/
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!