Rory
Rory

Reputation: 1825

Complicated regex - is this possible?

For the following strings, I am trying to extract 'mainline' if the string contains mainline, or the number at the end if it doesnt contain mainline. I am using Java.

Eg, for the following strings I want just the numbers at the end

Solaris10NBngp-bwm1.1.X     // want 1.1.x
Solaris10NBbytel2.0.0.0x    // want 2.0.0.0x
Solaris10NBbwm1.2.X         // want 1.2.X
Solaris10NBoam_bwm1.4.0.X   // want 1.4.0X
Solaris10NBoam1.7.X         // want 1.7.X

Mainline examples:

Solaris10NBngp-bwm_mainline // want mainline
LinuxNBdaypass_mainline     // want mainline
LinuxNBngp_mainline         // want mainline

Is this possible using regex, and if so, anybody know how to do it? :-)

Upvotes: 0

Views: 99

Answers (5)

Alexey
Alexey

Reputation: 919

See if this expression works

(\d\.)+\d?X$

Upvotes: 0

Aziz Shaikh
Aziz Shaikh

Reputation: 16524

Try this regex:

/(mainline)$|(\d+\..*)\s*/

If it finds mainline at the string end then $1 variable will contain the value mainline. If it finds a version number then $2 variable will be populated the $1 will be null/empty.

Upvotes: 0

Prince John Wesley
Prince John Wesley

Reputation: 63698

Solaris10NBngp-bwm1.1.X     // want 1.1.x 
Solaris10NBbytel2.0.0.0x    // want 2.0.0.0x 
Solaris10NBbwm1.2.X         // want 1.2.X 
Solaris10NBoam_bwm1.4.0.X   // want 1.4.0X 
Solaris10NBoam1.7.X         // want 1.7.X

For the above inputs,

expectedString = yourString.replaceAll("[^.]+(\\d+\\.)","$1");

Upvotes: 2

Ed Heal
Ed Heal

Reputation: 60007

Try this regular expression

([0-9.]+x) +.*$

NB Assuming the comments are a part of the string

Otherwise

[0-9.]+x)$

Upvotes: 0

Thomas
Thomas

Reputation: 88707

If the strings always start with Solaris10NB, try this expression: Solaris10NB[\w\-]+(\d.*).

Then extract the first group using Matcher#group(1).

Upvotes: 0

Related Questions