0

I have a string as

Celcom10 | 105ABCD | Celcom

This is my regex (\s\w+) however this also capture the white space.

How do I write a regex to obtain the middle values 105ABCD excluding the pipe and whitespace ?

I only need a regex pattern since this will be inserted within a diagram which will be executed in an automated test.

4
  • And what language are you using? Commented Jul 21, 2014 at 12:58
  • 2
    @abiieez Then split by \s*[|]\s*, you'll get an array Commented Jul 21, 2014 at 12:59
  • I cant use split function because this regex will be put within an ARIS diagram Commented Jul 21, 2014 at 19:01
  • @abiieez If that's the case then use [^\s|]+. But really, please try to clarify your question from the start by giving maximal information. Getting to the "real" question after 7 edits is a pain. Also if you're using some custom program, then why in the world are you asking us on how to get a specific group. Try to search on your own since it's not pure javascript. Commented Jul 21, 2014 at 20:20

3 Answers 3

3

You could try this also

[^ |]+
Sign up to request clarification or add additional context in comments.

2 Comments

How can I modify that to get the first, second or third only ?
I need only the middle value 105ABCD
3

This is pretty easy and straightforward if you split the string by: \s*[|]\s*

Explanation:

  1. \s* match optional whitespaces
  2. [|] match a literal pipe, | means or in regex but if you put it in a character class [] it loses it's meaning
  3. \s* match optional whitespaces

Sample code:

input = 'somenickname | 1231231 | brand';
output = input.split(/\s*[|]\s*/);

// Printing
for(i = 0, l = output.length;i < l;i++){
    document.write('index: ' + i + ' & value: ' + output[i] + '<br>');
}

Output:

index: 0 & value: somenickname
index: 1 & value: 1231231
index: 2 & value: brand

Comments

2

I need a regex (or maybe 3) to extract all the three words / number (excluding the whitespace and pipe)

Get the matched group from index 1. Here parenthesis (...) is used to capture the groups.

(\w+)

Live Demo

\w       match any word character [a-zA-Z0-9_]

Have a look at the sample code

5 Comments

What if these words has any special character?
Using a capturing group is redundant here.
that works thanks. Can I choose to capture the 2nd or 3rd occurrence only ?
Is the group index used to extract the which text matches ? I still cant figure it out
I need a regex, not split function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.