This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

How to merge regex inside java application?

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi :

I want to parse values from  text file the structure is listed bellow I took a portion from it




I choose the delimiter is DATA :

I want to get the following output inside textearea at once :

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

NB: data after INF may change for example :

INFO-1    or INFOG-1 or INFOT-1 or INFOP-1-1  etc.


2/ I have bellow regx :

DATA\s+NAME.*\nINFO-0\s+(?<name>[$\w]+).*\n.*\n.*VERAT2.*\n\s+\w+\s+(?<verat2>\w+).*\n.*\n.*CMD4.*\n(?:\s+\w+){3}\s+(?<cmd4>\w+)
INFOG-0\s+(?<mac1>[$\w]+)\s+(?<mac2>[$\w]+)
INFOC-0\s+(?<adrs1>\d+).*\n\s+\d+\s+(?<adrs2>\d+)
INFOP-0-(?:10|\d)\s+(?<namePlat>\w+)\s+(?:\w+\s+){3}(?<ti>\d+)\s+(?<val1>\d+).*\n.*\n.*\n.*(?<val2>\d+).*\n.*\n.*(?<val2>\d+)


3/  I did bellow code to achieve what I want , but it is so dificult to insert regx inside java eclipse mars 2.




NB:  who can help us and thanks in advance .

 
Sheriff
Posts: 6110
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

3/  I did bellow code to achieve what I want , but it is so dificult to insert regx inside java eclipse mars 2.


Can you tell us exactly what difficulties you're having?

Also, Mars is quite an old version of Eclipse, but I doubt that that is the problem.
 
Babi Astimal Astimal
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi again:

sorry , this is just we inform you that i'm using java eclipse .

I have posted my code and regx :

the question is who to put regx inside my java code .

thanks
 
Bartender
Posts: 2326
100
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean that you are having issues pasing regex while using eclipse ?
There is a config called "Escape text when pasting into a string literal" in eclipse. I think it should be under Preferences > Java > Editor > Typing.
 
Saloon Keeper
Posts: 2658
333
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Babi Astimal Astimal wrote:the question is who how to put regx inside my java code


I often use RegexPlanet to validate and fine tune REGEX expressions.  It also will provide you the escaped text specifically for Java.

I plugged-in your expression and it highlighted an error.

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

you are right Ron McLeod ( the question is how to put regx inside my java code ).

I put all regulars expressions into the site RegexPlanet :

the result show the following :  as a Java string

"DATA\\s+NAME.*\\nINFO-0\\s+(?<name>[$\\w]+).*\\n.*\\n.*VERAT2.*\\n\\s+\\w+\\s+(?<verat2>\\w+).*\\n.*\\n.*CMD4.*\\n(?:\\s+\\w+){3}\\s+(?<cmd4>\\w+) "
"INFOG-0\\s+(?<mac1>[$\\w]+)\\s+(?<mac2>[$\\w]+) "
"INFOC-0\\s+(?<adrs1>\\d+).*\\n\\s+\\d+\\s+(?<adrs2>\\d+) "
"INFOP-0-(?:10|\\d)\\s+(?<namePlat>\\w+)\\s+(?:\\w+\\s+){3}(?<ti>\\d+)\\s+(?<val1>\\d+).*\\n.*\\n.*\\n.*(?<val21>\\d+).*\\n.*\\n.*(?<val22>\\d+) "

now how to insert them all inside my java code ?

I choose one example in my code ( LEVELINFO=DATA=INFO-0&&NAME=EQUIP&&VERAT2=v22&&CMD4= BYTE )




in order to parse text file and get the result bellow at once  inside textarea :


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
 
Knute Snortum
Sheriff
Posts: 6110
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've taken your code fragment and written a compilable class.  Whether it actually works or not depends on several factors.  But it demonstrates how one could use several long regexes in a class.
 
Babi Astimal Astimal
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi      Knute Snortum:


thanks a lot of for your reply .


I have used bellow code but nothing was displayed inside textarea .



thanks

 
Knute Snortum
Sheriff
Posts: 6110
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have Scanner using a delimiter of "DATA", but REGEX_1 starts with "DATA".  Since "DATA" is the delimiter, the regex will never match.  I would try taking the "DATA" off REGEX_1:
Also, REGEX_1 has a space at the end.  That's probably not what you want.
 
Babi Astimal Astimal
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello:

I have corrected what was recomanded but nothing is displayed inside textarea :




thanks for your support and suggestion.
 
Knute Snortum
Sheriff
Posts: 6110
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I would start debugging the code.  I would put System.out.println("Found"); just after line 74 to see if find() found anything.  If it doesn't, I would start simplifying your regex until it does.
 
Babi Astimal Astimal
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello  Knute Snortum:

sorry to be late  this what I did but nothing was found :




NB: is there another way to parse this type of text file by another solution ( without regex) ?


Thanks
 
Knute Snortum
Sheriff
Posts: 6110
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally, I would start breaking REGEX_1 into smaller pieces and test each piece separately.
And then since you have these nice named groups, I would use them:
Once that works, add another piece of the regex.
 
Carey Brown
Saloon Keeper
Posts: 6042
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aside from the regex stuff, the layout of your input text file is not particularly organized. I would expect to see delimiters or fixed width columns. I wouldn't want to have to read that data as a human let alone write a regex for it.
 
Babi Astimal Astimal
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi again:

I did what was recomanded the problem still the same nothing was displayed .



NB: I put my text file in the link listed bellow:

https://www.cjoint.com/c/IHopJI1ik6A

thanks
 
Knute Snortum
Sheriff
Posts: 6110
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my entire program that matches one part of the data:
 
Carey Brown
Saloon Keeper
Posts: 6042
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Data file has a 'return' character (\r) in it.

 
Knute Snortum
Sheriff
Posts: 6110
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, that makes sense.  I'm on a Linux system so I didn't run into that problem.
 
Babi Astimal Astimal
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all:

now it works after have added \\r? inside regx .

I'm experiencing two problem now I didn't notice before :

1/  after the word  INFO- ( after dash the value change ) it can be INFO-0  or INFO-1  or INFO-60. I have changed the  REGX but nothing was displayed .

the value of INFO-0 is not parsed in order to get :

( LEVELINFO=DATA=INFO-0&&NAME=EQUIP&&VERAT2=v22&&CMD4= BYTE )



2/ when I put texTarea.append( "LEVELINFO=DATA=" + matcher.group( "name" ) );  instead of  System.out.println( "LEVELINFO=DATA=" + matcher.group( "name" ) );


fault code listed bellow is raised :
             




the full code is listed bellow:



thanks for support and suggestion .
 
Carey Brown
Saloon Keeper
Posts: 6042
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Babi Astimal Astimal
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello:

even with naming group nothing is displayed (even  the complication work 100%).




thanks
 
Carey Brown
Saloon Keeper
Posts: 6042
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
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Carey :

it works now but there is always new issue :

see bellow printout:



how to get the INFO- in order to have :

LEVELINFO=DATA=INFO-0&&NAME=EQUIP&&VERAT2=v22&&CMD4= BYTE .

2/  when I put textArea I have always bellow fault code :




3 /  see bellow new code :



thanks

 
Carey Brown
Saloon Keeper
Posts: 6042
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:how to get the INFO- in order to have :

Your error message tells you what is wrong, you don't have a group named "INFO-". You'll have to make one if you want.

You are attempting to debug one hellova nasty regular expression and a lousily formatted txt file. I suggest:
  • Don't attempt any output formatting at this point, e.g. LEVELINFO=DATA=INFO-0&&NAME=EQUIP&&VERAT2=v22&&CMD4= BYTE .
  • Don't use GUI code like Swing. Use a text interface.
  • Add debugging print statements to see what the input is from the file and dump the output to the console. My print() method may suffice but create more of your own if need be.

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


    I need to kown how to correct the Regx in order to add group name for INFO-:



    thanks

     
    Knute Snortum
    Sheriff
    Posts: 6110
    157
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The problem as I see it is that you have all sorts of INFO formats and each one will need a different regex.  Here are the INFO's that I see in your data:
    INFO-0, INFOG-0, INFOC-0, INFOP-0-0, INFOT-0
    and there may be more.  So I think the pseudocode for the matching part of your program will be:
    and so on.
     
    Carey Brown
    Saloon Keeper
    Posts: 6042
    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:I need to kown how to correct the Regx in order to add group name for INFO-:


    You've already used this syntax three times. You'll need to copy the layout of your other examples or look up the documentation for the syntax. When you embark on such a difficult regex problem you'll definitely need to know where the documentation is.

    Pattern class documentation. Book.

    The name must be enclosed in angle brackets:  ...<name>...
     
    Babi Astimal Astimal
    Greenhorn
    Posts: 15
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hi :

    I did bellow code but I have defined macher1 to the pattern another fault was raised ( scanner closed):



     
    Carey Brown
    Saloon Keeper
    Posts: 6042
    58
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    On line 103 you are closing sc. This is inside your while loop, so next time around the loop you no longer have access to the file.
     
    Marshal
    Posts: 65053
    247
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Don't try debugging code via a GUI. Separate the logic from the GUI, which is nothing but a display.
    Why did you close a Scanner? What was it reading from?
    Why are you combining Scanners and GUIs? Do all your reading from the GUI, via text fields or similar.
     
    Babi Astimal Astimal
    Greenhorn
    Posts: 15
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hi all:

    after made some correction only first REGX1 is displayed , but REXG2 no .

    NB:  I use graphic to copy the final script from textarea at the end to notepad and save it for further use.

     
    Carey Brown
    Saloon Keeper
    Posts: 6042
    58
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Babi Astimal wrote:NB:  I use graphic to copy the final script from textarea at the end to notepad and save it for further use.

    Why not just write it to a .txt file and be done with it?

    Besides, until you debug your regex you shouldn't be doing either.
     
    Knute Snortum
    Sheriff
    Posts: 6110
    157
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're looking to match this string:
    and you're using this regex:
    But the data that's supposed to be matched by group "mac1" ($1) doesn't have any characters that will match a word character (\w). That may be the problem. But you didn't specify if the exact problem is so I don't know.
     
    Carey Brown
    Saloon Keeper
    Posts: 6042
    58
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    A word character: [a-zA-Z_0-9]
    So, yes '$1' would match the OP's pattern.
    The problem is with spaces.
     
    Babi Astimal Astimal
    Greenhorn
    Posts: 15
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hi all:

    the problem was in space now the first regx and second are workin fine .



    2/ I'm working now on the third REGX but it is not working:



    in order to have this :

    LEVELINFO=DATA=INFOC-0&&ADRS=1&&4

    Im' triying to get the first number which is 1 and the lasr number which is 4  from ADRS


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

    I have tried with this :

    (?<infoc>INFOC-\\d{1,3})\\s+(?<adrs1>\\d{1,3}+).*" + NL  (  work fine ).

    but secand and third not OK .



    thanks
     
    Carey Brown
    Saloon Keeper
    Posts: 6042
    58
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You have two NL's but your data only has one. This is why debugging regex's is so critical, if you miss the slightest nuance in your input format it will fail.
     
    Consider Paul's rocket mass heater.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!