• 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

how can I extract string until any special character is met using regex?

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

I have a string that contains letters, numbers and special characters. I want to extract the first characters in the string until any special character is met.
I want to get from the beginning of the string the alphanumeric characters only. I will stop if any special character is encountered.

For example,

String msg = "1234567890ASDFGasdfg\ qwert#";

I want to extract "1234567890ASDFGasdfg"

Can someone please tell me what regex/pattern can I use?
 
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regex seems like overkill for this. Is the use of regex a requirement for the assignment or project?

There is an old saying from the Usenet days.

Some people, when confronted with a problem, think
“I know, I'll use regular expressions.” Now they have two problems

 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please explain what you want the all alphanumeric String for. There may be other ways to do what you want.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think your question is too long. It should be:

how can I extract string until any special character is met?

As soon as you say "using ABC", you've already decided how to do something without regard to whether it is the RIGHT way to do it or not. If I asked "How do I drive a nail into a board with a marshmallow?", you'd think i'm a little crazy. Why use a marshmallow? What's wrong with a hammer? I guess you could try freezing the marshmallow, but i'm still not sure it would work...

the first thing you should do is describe what you want to do - forget about how. The how should always be about the LAST thing you try and determine.
 
Saloon Keeper
Posts: 27764
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Everybody wants to tell you not to use a regex. And I agree. Regexes are a lot like nail guns. They can be extremely useful when applied properly, but I wouldn't use one to assemble a bird house. There's too much chance I'd nail my hand to my workbench.

Neverthess, I'm going to assume that you are actually required to use a regex. Could be a homework assignment. Could be a work assignment where the resident genius "knows" that a regex is the way it must be done. Whatever.

There isn't really a regex code for "special character" as such. There are too many different kinds of "special". But accepting the idea that "special" means non-alphanumeric and going with the premise that you're starting from the beginning of a string, here's one possibllity:

Look for the largest continously alphanumeric sequence: "^([\p{alnum}]*)" - remember that in an actual Java string expression, you have to double-up on the backslashes. This regex only looks for USA ASCII simple letters and numbers - to it, the extended letters and other characters are "special"

Another option looks for word-like charactersL "^(\w*)". However, that one includes the underscore character, which means that it might not be "special" enough.

You can also do brute-force: "^([a-zA-Z0-9]*)".

Various other regex options exist. Note that there's also no "universal" regex language definition set, so what's a vaiid regex in Java might not be valid using the Unix grep utility or vice versa.

Then again, as others have said, regex isn't always the most elegant solution to string parsing. Various other parsing and scanning services exist in Java and might be simpler to use, depending on your exact needs and constraints.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:. . . Another option looks for word-like charactersL "^(\w*)". . . .

Wouldn't you use the uppercase equivalent "\\W+" for non‑word characters?
 
Tim Holloway
Saloon Keeper
Posts: 27764
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Tim Holloway wrote:. . . Another option looks for word-like charactersL "^(\w*)". . . .

Wouldn't you use the uppercase equivalent "\\W+" for non‑word characters?



Actually, more like "^(\w*)\W". Except that I'm not sure if the end-of-string counts as a "special character" for the regex processor in question. So maybe \W* at the end of the regex.

Note that I didn't rule out an expression that begins with a special character (thus returning an empty-string match).
 
But how did the elephant get like that? What did you do? I think all we can do now is read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic