• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regex for matching mobile topup codes and Url

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've been trying to get the start and end position of string from a sentence using Matcher Class . so far I have wrote the following regex for finding the following strings:


this works fine when a single word is entered but not when i try to find those words in a sentence by combining the regex like this,

Combined Regex


I don't know what i'm doing wrong and i assume there should be a better way for implementing this.

The Code


this is used in android , the found string gets clickable by using ClickableSpan Class.

Examlple String


 
Saloon Keeper
Posts: 7602
177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you describe in words what exactly you're trying to find?
 
dev goku
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to get the specific words from the given example sentence that match the combined regex.
 
Marshal
Posts: 79475
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will definitely get whatever matches the regex, but that doesn't answer Tim's question. You need a description of what you want before you try to work out how to get it.
 
dev goku
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright, I'm a android developer, I'm trying to make the part of text clickable that matches the regex by getting start and ending index of the text from a sentence, you can find the strings that needs to be found (*1111,*1111#,numbers, url). You can also see the regex for the individual words and I combined them to be able to use it.
 
Tim Moores
Saloon Keeper
Posts: 7602
177
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you should start by getting the underlying regexp right - the dots in .* look fishy - I think you mean [0-9]* rather than [0-9].*. If you want to ensure that at least a single digit is matched then [0-9]+ would be the right way to do that rather than [0-9]*[0-9].

A site like http://www.regexplanet.com/advanced/java/index.html can help you get complex regexps right.

For Android specifically, the android.text.util.Linkify class can help you greatly: https://android-developers.googleblog.com/2008/03/linkify-your-text.html
 
dev goku
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, i will look into it.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic