• 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
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Help with Regex Logic  RSS feed

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there!

I have this line: 12894  | 'Coke 2L'   |   Dept[Drinks]   |   Price (10.0)   |   Stock (64)

And I just need the ID: 12894

I'm using this regex: [^0-9.]*\([^)]*\) *

Using the previous Regex:


Without the 2:



How should it be the Regex to work the way I need?
 
author
Posts: 23804
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

David Martínez wrote:
How should it be the Regex to work the way I need?



First, with regular expressions, it isn't as simple as the regex string itself. You need to tell us how is the regular expression being applied.

And second, you need to tell us which regular expression you are using -- as regular expressions are available in a multitude of languages and frameworks. At this point, I can clearly say that this question is in the wrong forum, but I can't say where it should be moved to!!

Henry
 
David Martínez
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:

David Martínez wrote:
How should it be the Regex to work the way I need?



First, with regular expressions, it isn't as simple as the regex string itself. You need to tell us how is the regular expression being applied.

And second, you need to tell us which regular expression you are using -- as regular expressions are available in a multitude of languages and frameworks. At this point, I can clearly say that this question is in the wrong forum, but I can't say where it should be moved to!!

Henry



I know this is not the correct forum, but I tought I chose Beggining Java

I got a workaround using this expression \\|.*$", ""


With the previous line of code, it removes everything when it finds a '|' character, then using StringUtils  I can remove the remaining Whitespaces.
 
Henry Wong
author
Posts: 23804
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

David Martínez wrote:
I know this is not the correct forum, but I tought I chose Beggining Java



Okay, I will move this topic to the Beginner Java forum for you.

David Martínez wrote:
I got a workaround using this expression \\|.*$", ""



Okay, now this makes much more sense. Normally, when discussing regular expressions, it is in the context of the matches(), find(), or even, the split() method. So, without the mention of the replaceAll() method, the original question was very confusing ...

Henry
 
Greenhorn
Posts: 19
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

David Martínez wrote:Hello there!

I have this line: 12894  | 'Coke 2L'   |   Dept[Drinks]   |   Price (10.0)   |   Stock (64)

And I just need the ID: 12894

I'm using this regex: [^0-9.]*\([^)]*\) *

Using the previous Regex:


Without the 2:



How should it be the Regex to work the way I need?



The Regex, that will work for you can be coined in several ways:
You could you use:



which is also the same things as this



EXPLANATION:
You take the first group, starting from the start of the string, take from 1 to several of the "integer" which ranges from 0 to 9.
That is the group you wanted and so you can then print it out.

I will also suggest that you take a look at this https://docs.oracle.com/javase/tutorial/essential/regex/index.html
 
David Martínez
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

timothy adigun wrote:

The Regex, that will work for you can be coined in several ways:
You could you use:



which is also the same things as this



EXPLANATION:
You take the first group, starting from the start of the string, take from 1 to several of the "integer" which ranges from 0 to 9.
That is the group you wanted and so you can then print it out.

I will also suggest that you take a look at this https://docs.oracle.com/javase/tutorial/essential/regex/index.html


Thank you for your help! Will look into that.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!