• Post Reply Bookmark Topic Watch Topic
  • New Topic

regex for ADA.  RSS feed

 
Roopesh Rana
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.

 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • 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 
 
Tim Moores
Saloon Keeper
Posts: 4024
94
  • 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
Sheriff
Posts: 4917
334
BSD
  • 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
    Sheriff
    Posts: 4917
    334
    BSD
    • 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.
     
    Jesper de Jong
    Java Cowboy
    Sheriff
    Posts: 16059
    88
    Android IntelliJ IDE Java Scala Spring
    • 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.
     
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12563
    49
    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.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!