Hi all!

I need to create regex that matches everything before some specified (whole) word. Lets say I need everything before 'have' in such text:

What ^7^*#/> I @@3->;: have to do with this *~~@}}], hmm...?

...so I would like to have "What ^7^*#/> I @@3->;: " as a result - weird symbols inside the example are just to show what are we dealing with (it is not plain text, like "plain text")

It seems not so easy, though...
ideas?

You have asked this question in multiple forums. First decide what language you want use before you go any further.

Yes, the regex is easy, but the code you use makes a difference.

Just use (.*)have . That will match everything up-to and including the "have". The first "capture", or subexpression, is everything except the "have".

In fact, if the OP doesn't know how to use regex, let's be honest it doesn't look like he does, why not just use simple string functions?

True. Using a regex would definitely be overkill just to get what he wants...

Just find where in the string "have" starts, and the answer is everything before that...

But like iamthwee advised, you need to decide what language you want to use to accomplish your goal.

Yes, the regex is easy, but the code you use makes a difference.

Just use (.*)have . That will match everything up-to and including the "have". The first "capture", or subexpression, is everything except the "have".

It actually doesn't matter in what language, because if I get the 'idea' I would be able to convert it so it suits my needs. I understand the difference... regex differs slightly between languages. In this case I need it for MS C#, but I would also like to know how to do this in Java, C++ and Python, because it is not the first time I have this problem.

I posted on many forums so I would get answer faster or get it at all, I didn't know that there is somebody reading them all (sorry to you all :))

I have by now walked round this problem... again.
Thanks for contributions, though!

True. Using a regex would definitely be overkill just to get what he wants...

Just find where in the string "have" starts, and the answer is everything before that...

But like iamthwee advised, you need to decide what language you want to use to accomplish your goal.

It would be overkill to use regex if the whole problem was like the example above. I regret it is not. What I asked was supposed to be a part of bigger solution - that's why regex :))

Not being funny, but the syntax for regex differs subtely from language to language, so you will probably get conflicting solutions - it might even confuse you.

And posting (spamming) on multiple forums is more annoying than anything else.

Comments
I'm annoyed enough to completely ignore the OP

You know, there is a whole web site devoted to regex: RegularExpressions.info.

The two most complete standards are Perl and POSIX. Perl is better, and the two diverge a bit, so given the choice choose Perl or Perl Extended.

Most other regexp engines are underpowered and/or domain-specific. (AFAIK)

I think I have visited 'RegularExpressions.info' site earlier, but didn't find what I needed, or just didn't understand what did I find... :-) Nevertheless, thanks for the link and, oh... I'll try not to post on several forums again... sorry... again.

Have a nice day!

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