Gabriel Shen
Gabriel Shen

Reputation: 99

Searching for first parenthesis within a string

I have different types of strings which are like the following:

I want to check if the value between the parenthesis is a number. But only for the ones that appear right before the 4 digit number at the end. So the 3rd on with an integer in the beginning of the string won't be checked. I'm using Javascript and was trying to using string.search(). I'm thinking of searching for ")(". I tried doing string.search("[)(] ") but it's not giving me the right position or searching it correctly. Does anyone have any recommendation on how to search for )( or some other way to get that value?

Upvotes: 2

Views: 155

Answers (3)

PhantomSpooks
PhantomSpooks

Reputation: 3570

If you need to parse the numbers from the string you can use String.match, which accepts regular expressions and returns the sub-strings matching that regular expression:

let strings = [
  "Amount of Apples(1)(1234)",
  "Amount of Pears(2)(2345)",
  "(2)Amount of Bananas(2344)",
  "Amount of Oranges(Green)(3455)"
]
//regex for 4 digits
let re = /\d{4}/
let numberFromStrings = strings.map(str=>{
  // str.match returns array
  let results = str.match(re)
  // if the results is 0 there is no match
  if (results.length == 0)
    return false  
  return results[0]
})

console.log(numberFromStrings)

This way, if the regex matches, you can parse the values you need and check the string all in one swoop

Upvotes: 0

Ryszard Czech
Ryszard Czech

Reputation: 18641

Use

/\(\d+\)\(\d{4}\)$/.test(string)

See proof.

Explanation

--------------------------------------------------------------------------------
  \(                       '('
--------------------------------------------------------------------------------
  \d+                      digits (0-9) (1 or more times (matching
                           the most amount possible))
--------------------------------------------------------------------------------
  \)                       ')'
--------------------------------------------------------------------------------
  \(                       '('
--------------------------------------------------------------------------------
  \d{4}                    digits (0-9) (4 times)
--------------------------------------------------------------------------------
  \)                       ')'
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string

JavaScript code

const array = ["Amount of Apples(1)(1234)","Amount of Pears(2)(2345)","(2)Amount of Bananas(2344)","Amount of Oranges(Green)(3455)"];

const regex = /\(\d+\)\(\d{4}\)$/;

array.forEach ( x =>
  console.log(`${x} matches the regex: ${regex.test(x)}`)
)

Upvotes: 0

esqew
esqew

Reputation: 44722

You can use RegExp for this purpose. Simply use RegExp.prototype.test() to check if a given string matches the expected pattern (digits appearing before the four-digit number enclosed in parenthesis at the end of the string):

const stringsArray = ["Amount of Apples(1)(1234)","Amount of Pears(2)(2345)","(2)Amount of Bananas(2344)","Amount of Oranges(Green)(3455)"];

const pattern = /\(\d+?\)(?=\(\d{4}\)$)/; // for further explanation of this, see https://regex101.com/r/PfBnzk/1/

for (string of stringsArray) { // loop over every string in the array
  const matchResult = pattern.test(string); // get a boolean TRUE or FALSE of whether or not the string matches the pattern
  console.log(string + " " + matchResult); // print the result of the match to the console
}
  


For a more colloquial explanation of the pattern I used, you can check out this Regex101.

Upvotes: 2

Related Questions