Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (481)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (547)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Simple regex help  (Read 2995 times)
0 Members and 1 Guest are viewing this topic.
Offline dranonymous

Junior Member




Hoping to become a Java Titan someday!


« Posted 2006-09-14 19:26:56 »

Can some regex guru tell me the magic incantation to match this situation -

- find the word happy

- ignore unhappy

- match happy starting or ending with anything else

examples which should match -

###happy###
-- happy --
   happy

I'm trying to search through some files in linux using grep, but I can't figure out how to construct the regex to do that.

Regards,
Dr. A>
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 781
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2006-09-14 20:56:53 »

the 1st condition looks like it's already found by the 3rd, so basicly you want:

pseudo regex (i don't know regex...)
"*happy*" minus "unhappy"

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline dranonymous

Junior Member




Hoping to become a Java Titan someday!


« Reply #2 - Posted 2006-09-14 21:03:59 »

I'm confused.  "*happy*" minus "unhappy"  doesn't look like a regular expression (regex)

Some examples of a regular expression would be -

[01234]   which would match a single number between 0 - 4
[0-4]  the same as above
\bword\b  matches word with nothing next to it, ie it can't be in the middle of other text

Thats about the sum total of my regex knowledge, hence my please for help.

Dr. A>
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline cylab

JGO Ninja


Medals: 43



« Reply #3 - Posted 2006-09-14 23:22:19 »

there is no "not" operator in standard regex... I would suggest to chain two greps - the last one with "-v" for  inverted matching:
1  
echo -e "I seem unhappy\nbut am really happy" | grep 'happy' | grep -v 'unhappy'

results in
1  
but am really happy


Mathias - I Know What [you] Did Last Summer!
Offline f.l.x

Senior Member


Projects: 3


there is no place like 127.0.0.1


« Reply #4 - Posted 2006-09-15 00:48:29 »

if you are looking for something more jflex-like it would be something like

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
TK = "happy"
%%
// first so the lexer looks for this before looking for ~happy~
"unhappy" {
    ;// nothing here so we ignore it
}

.*{TK}.* {
    // here your lexer code, f.e. System.out.println(yytext());
}

90% inacurate since my flex skills are disapearing like sand in the wind.

Litterarum radices amaras, fructus dulces
http://flx.proyectoanonimo.com
figth spam!
Offline endolf

JGO Coder


Medals: 7


Current project release date: sometime in 3003


« Reply #5 - Posted 2006-09-15 06:51:32 »

Hi

I've your using grep like you say you are, then cylab wins the cookie.

1  
grep happy <file list> | grep -v unhappy


Endolf

Offline chrm

Junior Newbie





« Reply #6 - Posted 2006-09-15 09:37:12 »

\b([^ ]*([^u]n|u[^n]|[^un][^un]))?happy[^ ]*\b

\b   A word boundary
[^abc]   Any character except a, b, or c (negation)
X*   X, zero or more times
X|Y   Either X or Y
(X)   X, as a capturing group
X?   X, once or not at all

http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Pattern.html

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 781
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #7 - Posted 2006-09-15 15:41:20 »

With such expressions it's very tempting to just String.indexOf your way out Lips Sealed

Regex may be very powerful, but designing, debugging and maintaining of any non-trivial pattern is not worth it IMO (unless it's an exercise)

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline f.l.x

Senior Member


Projects: 3


there is no place like 127.0.0.1


« Reply #8 - Posted 2006-09-15 17:06:25 »

it's with non-trivial patterns where regexp are usefull, with things like the "happy" thing it's like using a cannon to kill a flea.

Litterarum radices amaras, fructus dulces
http://flx.proyectoanonimo.com
figth spam!
Offline Breakfast

Senior Member




for great justice!


« Reply #9 - Posted 2006-09-16 23:23:22 »

Regexps are fantastic. One of the most useful tools we have, but pretty much a programming language in their own right.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline dranonymous

Junior Member




Hoping to become a Java Titan someday!


« Reply #10 - Posted 2006-09-17 20:16:09 »

This isn't a java project its so I can search through files on the command line and figure out which ones I need to examine.

I think cylab has the solution for me.  I can remember it for other cases!

Much thanks to all for their input.

Dr. A>


PS - <troll bait> While I understand how powerful regex-s can be, I think they are evil, just like perl!! </troll bait>
Offline Breakfast

Senior Member




for great justice!


« Reply #11 - Posted 2006-09-17 21:07:41 »

Ah, young padawan, once I was like you. They are not evil, though they are subtle and quick to anger. The real problem with them is that they are powerful and if you are weak and tempted by the dark side you could easily be consumed by their greedy evaluation. If you are afraid of them, you aren't ready to face them yet. Don't worry about it, they will still be there when you are ready.

Perl though, that is evil...

Offline ryanm

Senior Member


Projects: 1
Exp: 15 years


Used to be bleb


« Reply #12 - Posted 2006-09-17 22:46:36 »

Truly, sir, that was an admirable reply. You are indeed worthy of the epithet "ninja".

Whether you will be chosen by the Java Core though, that is not for mortals to decide...

(Yes, I'm still curious)
Offline Anon666

Junior Member




aka Abuse/AbU5e/TehJumpingJawa


« Reply #13 - Posted 2006-09-18 00:15:46 »

Someone needs to devise a verbose regex syntax.

The logic behind construction of an expression is fundamentally less complex than programming, only the obtuse syntax scares people off (and rightly so IMO).
Give me an eclipse GUI plugin for building & maintaining them, and I might use them for more than trivial string spliting.
Offline dranonymous

Junior Member




Hoping to become a Java Titan someday!


« Reply #14 - Posted 2006-09-18 15:11:04 »

Breakfast you are truly a Phoentic Warrior!

I label regex-s as evil, much the same way I label lex and yacc.  Everytime I try to learn about them, the description is just as confusing as the syntax.  I would learn far better with a bunch of examples and their results.  If I could just find that mixed in with the traditional explanations, I'd probably avoid the hate completely.

I recently used the Spirit library from Boost and found it to be excellent.  While not everything was intuitive to me, creating parsers and grammer rules just made sense.  Some day my Jedi powers will be strong enough to bring balance to my programming.  Until then, I'll keep using my Goto statements.  Smiley

Cheers,
Dr. A>
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

atombrot (26 views)
2014-08-19 09:29:53

Tekkerue (24 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (13 views)
2014-08-16 06:20:21

Tekkerue (20 views)
2014-08-16 06:12:11

Rayexar (58 views)
2014-08-11 02:49:23

BurntPizza (38 views)
2014-08-09 21:09:32

BurntPizza (30 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!