• 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
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

regex for ADA.

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have a string "ADA.3". After "." number could be anything.

I need regex for this.

 
Marshal
Posts: 7787
536
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please show us what have you tried so far.

Apart from that. You didn't provide sufficient information such as:

Does your string start with that?
Or can be in a middle of string too?
Or maybe at the end?
Or there is one string with just ADA.*?
Or there is one string with multiple ADA.* inside?

Why do you need regex at all, maybe contains("ADA.") would work?
 
Roopesh Rana
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My string start with "ADA."

Number would be change like "ADA.1", "ADA.2".

contains("ADA.") will not work, if my String is "MADA." show contains will return true.


 
Roopesh Rana
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

there is one string with just ADA.*

(*) will be replaced with a number  
 
Saloon Keeper
Posts: 6630
161
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the "ADA." part needs to be at the start of the string? Then startsWith rather than contains should work.

The regexp pattern can be anchored to the start of the search string by using the ^ special symbol.
 
Liutauras Vilda
Marshal
Posts: 7787
536
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what have you tried?

What we know so far:
  • Starts with "ADA."
  • Contains any number after the mentioned prefix
  •  
    Liutauras Vilda
    Marshal
    Posts: 7787
    536
    Mac OS X VI Editor BSD Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Tim Moores wrote:Then startsWith


    I guess OP will say it can't, because can be "ADA.MAMAMA" while there supposed to be any number after the "." (dot).

    But I agree with you, it is difficult to help when need to pull out requirements or worse, guess on them.
     
    Java Cowboy
    Posts: 16084
    88
    Android Scala IntelliJ IDE Spring Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    We still need a lot more information about the actual problem to be able to give you a useful answer.

    So, you have a string that starts with 'ADA.' followed by a number. And you want a regex for that.

    Why are you saying you need a regex? What do you exactly want the regex for? What do you exactly need to do with the string? Do you need to extract the number, or something else?

    If you just want the number, and the string always starts with "ADA.", then why bother with regular expressions anyway? It would be much easier to do int number = Integer.parseInt(text.substring(4)); to get the number as an int.

    Please explain exactly what the string might or might not contain, what you are trying to do with it and why you think you need a regex; and what you need that regex for.
     
    lowercase baba
    Posts: 12911
    63
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The hardest part of writing a regular expression is defining what you want. I always like to spell it out in words, describing each part. So I might say i only match if i have:

    The beginning of the string, followed by
    the literal string "ADA.", followed by
    a single digit, followed by
    the end of the string

    The challenge becomes changing the above to meet your actual requirements - which we don't know.  is a single digit all that's allowed?  or is there a maximum number of digits? could there be no digits? do the digits end the string? etc.

    Try writing out your requirements like the above and post them here.
     
    Why should I lose weight? They make bigger overalls. And they sure don't make overalls for tiny ads:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic