This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Beginner Regex Question

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How would I write a regular expression to only look for the 4?

I want it to confirm that the 4 is either a [4|5] it can ignore all the digits after it

2 0.4995
 
Sheriff
Posts: 4644
582
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Juan González wrote:How would I write a regular expression to only look for the 4?


Do you just want to know if a 4 exists in the string?
 
Juan González
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to know if the 4 exists in that exact position

for more context I have this:

2 0.4995
3 0.3239
4 0.2541
5 0.2049
6 0.1653
7 0.1467
8 0.1279
9 0.1153
10 0.1044

I want to check if the number after the . is a 4 or a 5

I apologies if this doesn't make any sense
 
Saloon Keeper
Posts: 15730
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ron McLeod
Sheriff
Posts: 4644
582
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use a tool like the one at RegexPlant to experiment different expressions and see what the results would be with a variety of inputs.

In this example, I specified a expression which was a literal dot (which needed to be escaped because it has a special meaning) and the number four.



Edit: sorry - I didn't notice that you wanted .4 or .5 - obviously the example above would need to be altered slightly to match
 
Juan González
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for the help, I really appreciate it!
 
Saloon Keeper
Posts: 28321
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, it doesn't make sense as an isolated question, but we'll assume you have a practical need.

To do what you need, you need advanced regex features that can look at the match or lack thereof and its context. For example, if your regex needs to match a "\\.[45]", which is a Java source string pattern for decimal followed by 4 or 5, you can use the match API to find the substring that preceded the match, the substring that comprises the matched character sequence, and the substring that follows the matched character sequence.

You can do very powerful things like match and capture values from multiple sub-patterns in your match pattern as well, but that's more than you need at the moment.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic