• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

How to merge regex inside java application?

 
Bartender
Posts: 6115
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my approach to handling multiple regex's: I built a helper class called "Group" that is easily processed in a loop. And to reiterate: no GUI and no formatted output at this point.

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Carey:

it works fine thanks a lot of .

I have two other questions :

1/   now I will add another regx but I want to make a loop to parse all   ( INFOP-) at once because the code display only  INFOP-0-0  and stop .

NB: INFOZ and INFOY  and INFOD their  related REGX will be added later and may be it is the same case with INFOP ( trough a loop)

I will add a new text file to explain that  ( https://www.cjoint.com/c/IHwmfnn17J5).

firstly bellow what I did:





2/  can we count the number of  EQUIPW by taken as condition only the last char ( W) .

for example in my DATA1 there is two EQUIPW  so I put the result in Jedittext or jlable ( found two )

if I change EQUIPW by EQUIPZ now the last char is changed to Z I will get ( one found EQUIPZ )

thanks


 
Carey Brown
Bartender
Posts: 6115
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • Please shorten your lines before posting them. Long lines are difficult to read on narrower screens.
  • You were missing group "ti".
  • You don't need non-capturing groups because you only care about the named groups.
  • If you want to collect all INFOP groups together make a list of all found groups and then provide logic to pick the groups you want to print to a txt file.


  •  
    Babi Astimal Astimal
    Greenhorn
    Posts: 28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hi Carey:

    lines are  just  long for explanation purpuse  but it is noted .

    1/ can you explain the use of breck in your code ?




    2/  how to make a list of all found groups and then provide logic to pick the groups you want to print to a txt file?

    thanks
     
    Babi Astimal Astimal
    Greenhorn
    Posts: 28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hi carey:

    your code work fine I didn't pay attention all INFOP are gathered at once .

    now just for design purpuse I want my result will be displayed inside JtextArea with  use of JRadioButton  in order to get bellow  information can you help us ?


    LEVELINFO=DATA=INFO-0&&NAME=EQUIP&&VERAT2=v22&&CMD4= BYTE
    LEVELINFO=DATA=INFOG-0&&MAC1=$1&&MAC2=IPARP  
    LEVELINFO=DATA=INFOT-0  
    LEVELINFO=DATA=INFOC-0&&ADRS=1&&4
    LEVELINFO=DATA=IINFOP-0-0 &&NAMEPLAT=EQUIPW&&TI 0+&&VAL1=10&&VAL2=11&&13

    thanks
     
    Carey Brown
    Bartender
    Posts: 6115
    58
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Babi Astimal Astimal wrote:can you explain the use of breck in your code ?

    The loop tries each regex in order. When you find a regex that matches then none of the remaining regexs can possibly match so there's no point in checking them. Break causes loop to abort because we already found one.
     
    Marshal
    Posts: 65366
    248
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I should have moved this discussion to our “general” forum a long time ago.
     
    Sheriff
    Posts: 6163
    163
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Babi Astimal Astimal wrote:now just for design purpuse I want my result will be displayed inside JtextArea with  use of JRadioButton  in order to get bellow  information can you help us ?


    Please ShowSomeEffort (that's a link) to do the project yourself.  Try coding it, then post any difficulties you are having.
     
    Babi Astimal Astimal
    Greenhorn
    Posts: 28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hi Knute Snortum  :

    I have followed your advices from the begining

    I have followed carey's advices also that was the answer listed bellow :


    This is my approach to handling multiple regex's: I built a helper class called "Group" that is easily processed in a loop. And to reiterate: no GUI and no formatted output at this point.



    I have posted more codes which show that i'm trying to do something.

    carey code work fine but I can't find how to keep it and add textarea and button tha's why I asked help.



     
    Knute Snortum
    Sheriff
    Posts: 6163
    163
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'm assuming that JTextArea has some setText() method.  Have you tried that?

    Please TellTheDetails (that's a link) of the problem you're having.  I'm assuming you're writing a Swing program.  Have you written one before?  What resources and tutorials have you looked into?  Have you looked at this:

    https://docs.oracle.com/javase/tutorial/uiswing/index.html
     
    Babi Astimal Astimal
    Greenhorn
    Posts: 28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hi Knute Snortum:

    yes I have already started using textarea from my first message :

    textarea.append().



    Now how to put or call bellow code inside  the code listed above ?

     
    Knute Snortum
    Sheriff
    Posts: 6163
    163
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Just copy and paste it into your class, like it was a method.  I usually put them at the bottom, some people put them at the top, but anywhere will do.
     
    Babi Astimal Astimal
    Greenhorn
    Posts: 28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have this message fault error:

    Exception in thread "main" java.lang.ExceptionInInitializerError
    Caused by: java.lang.NullPointerException
    at java.util.regex.Pattern.<init>(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)
    at rexdemo4$Group.<init>(rexdemo4.java:41)
    at rexdemo4.<clinit>(rexdemo4.java:28)



    sorry I have to put the whole code thanks to correct me :

     
    Knute Snortum
    Sheriff
    Posts: 6163
    163
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It looks like you've forgotten to set REGEX_1 etc. to your regex string.
     
    Babi Astimal Astimal
    Greenhorn
    Posts: 28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hi Knute Snortum  :

    I have followed the correction done by the compiler .

    Now I have corrected it but I still get the same error :



    the Error:

    Exception in thread "main" java.lang.ExceptionInInitializerError
    Caused by: java.lang.NullPointerException
    at java.util.regex.Pattern.<init>(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)
    at rexdemo4$Group.<init>(rexdemo4.java:41)
    at rexdemo4.<clinit>(rexdemo4.java:28)
     
    Carey Brown
    Bartender
    Posts: 6115
    58
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Knute Snortum wrote:It looks like you've forgotten to set REGEX_1 etc. to your regex string.

     
    Babi Astimal Astimal
    Greenhorn
    Posts: 28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    ah ok I missed it .

    Now error has gone but one I click on run button :

    I have a result and  another  error is displayed even info group is declared




     
    Carey Brown
    Bartender
    Posts: 6115
    58
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You need a different print format for each regex, not all of them have a group named 'info'.
     
    Babi Astimal Astimal
    Greenhorn
    Posts: 28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    like this but I still get error :



    error:

     
    Carey Brown
    Bartender
    Posts: 6115
    58
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I was trying to nudge you in the direction of using an object oriented approach. You were proceeding down a path of redundant, cut-n-paste code, which, while it would work, is not object oriented. One feature of object oriented design is encapsulation. As an example, Group encapsulates the finding and printing so that the code needn't be duplicated for each regex. The side effect of this is that "Group" is now shared among all patterns and any changes to Group need to be done in a way that extends to all regex's. In your case you would need to pass in an additional argument for a format to be used to print.

    It seems that you might not be ready for this yet. I was hoping that by giving you an example that you might have been able to learn from it.

    So, back to procedural coding. I suggest that you put this code aside and start over. You can reuse, of course, the REGEX_#'s that you created and have now debugged. Remember that debugging was a major goal at the time. Forget using Group for now, but I do recommend creating a separate method for each regex. There will be some cut-n-paste redundancy between them but that is the price you pay.
     
    Babi Astimal Astimal
    Greenhorn
    Posts: 28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hi carey:

    sorry for my late reply i was sick.

    yes as a beginner i have asked help nothing more and all your advice helped me you and Knute Snortum also and thanks for that.

    i will use the first code and it work 70% .

    NB : shall I ask help again?
     
    Knute Snortum
    Sheriff
    Posts: 6163
    163
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Babi Astimal Astimal wrote:shall I ask help again?


    Sure.  Just keep in mind that people are more willing to help if you show some effort.
     
    Babi Astimal Astimal
    Greenhorn
    Posts: 28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hi knute:

    thanks a lot of for your reply:

    it remains only one thing and i stuck on it .

    this a link for my test:  https://regex101.com/r/OxoKwE/1.

    the regx match all line of INFOP in the link listed above but when I run the code it parse only one line for example i have tried this :

    [infop="INFOP-0-0"
    namePlat="EQUIPW"
    ti="0"
    val1="10"
    LEVELINFO=DATA=INFOP-0-0 &&NAMEPLAT=  EQUIPW&&TI=0&&VAL1=10



    NB: i have tried to make  a loop to parse all line but no result what can I do now ?

    thanks

     
    Carey Brown
    Bartender
    Posts: 6115
    58
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    A loop is on the right track. Can you show us what you tried? Are you looping for all 'data' found?

    Please fix your indentation. Show some effort.
     
    Babi Astimal Astimal
    Greenhorn
    Posts: 28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    the loop is only for :



    2/ the data found after executing of the loop ( the first line repeated 3 times ) it dosen't take the next line matched with regx ( link attached).



    3/ I have tried to make add  ( data.equals("INFOP-") and remove the loop   but i get only one line :

     
    Carey Brown
    Bartender
    Posts: 6115
    58
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Babi Astimal Astimal wrote:3/ I have tried to make add  ( data.equals("INFOP-") and remove the loop

    Why?

    but i get only one line

    Of course you would.

    FIX YOUR INDENTATION. Post more of the code so that we can see the context, e.g. where does 'data' come from?
     
    Knute Snortum
    Sheriff
    Posts: 6163
    163
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If you need help with HowToFormatCode, click the link.
     
    Babi Astimal Astimal
    Greenhorn
    Posts: 28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    this what you asked after formating the text :




    thanks for the trick knute .
                                                                   
     
    Carey Brown
    Bartender
    Posts: 6115
    58
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    MUCH better formatting!

    Line 10 will never be true. You could replace it with:
    or you could just let line 14 make that determination for you.
     
    Think of how stupid the average person is. And how half of them are stupider than that. But who reads this tiny ad?
    Enterprise-grade Excel API for Java
    https://products.aspose.com/cells/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!