简体   繁体   中英

regular expression to match pattern in string

Below is the sample words that I will use at time of file import

  • East Chesterton (Cambridge)
  • New york (USA)
  • child (parent)

So here are the business rules:

  1. First word should be at least 3 chars long (le child)
  2. Allow space , but it's invalid if there's only space (le East Chesterton)
  3. The other part of the word is in ( someword )
  4. The ( someword ) is optional
  5. If ( someword ) is there it's minimum length is of 3, and spaces are also allowed.

I have achieved this at some level using following expression:

^[a-zA-Z ]{1,}\([a-zA-Z ]{1,}\)$
  1. Now i want to make sure this is correct expression. Is there any way to check with automation to check multiple combination to verify my expression?

  2. How i can achieve optional part (point no. 4), It min whether i pass (somedata) or not that check for first part.

Also to extract data in '( )'

\((.*?)\)

I think you are almost there. I did a try. Does this comply to all your requirements?

^[a-zA-Z\s]{3,}(\([a-zA-Z\s]{3,}\))?$

https://regex101.com/r/yE9lB0/2

I made the second part optional by putting it in between parenthesis and adding a question mark at the end: (myoptionalexpression)?

I've taken a look at the answer posted here.

 ^[a-zA-Z\s]{3,}(\([a-zA-Z\s]{3,}\))?$

This would clash with

  • Allow space , but it's invalid if there's only space (li East Chesterton)

Only empty spaces would already match.

Besides that the description 'characters' might be a bit vague. I have therefore assumed word characters \\w are what you mean. (in C sharp \\w should include unicode characters like ü as well. Think of Münster (Germany) as example.

The new regex would look like this:

^\s*(?:\w{3,}\s*)+(?:\(\s*(?:\w{3,}\s*)+\))?\s*$

Examples here: https://regex101.com/r/gS7kG8/3

Note that the regex101 page works with php,python and js regex, it will not give exact results in case of C# ( \\w in php apparently doesn't match unicode for instance)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM