0

I've a strings which look like:

array(
    'a few words 10g some more words',
    'a few words 100 g some more words',
    'a few words 100g/25mg some more words',
)

and I'm trying to extract the number which preceeds the letter g. In some of the strings the number and letter g will have a space between them, in other cases they will be beside each other.

What regex do I need to use to match either pattern?

crazy sarah
  • 641
  • 4
  • 11
  • 26
  • 1
    Looks like you are looking to create a regex, but do not know where to get started. Please check [Reference - What does this regex mean](https://stackoverflow.com/questions/22937618) resource, it has plenty of hints. Once you get some expression ready and still have issues with the solution, please edit the question with the latest details and we'll be glad to help you fix the problem. – Wiktor Stribiżew Nov 05 '19 at 21:17
  • Will it always end with a G? If so, try [`(\d+ *g)`](https://regex101.com/r/wS9dTU/1) - If not, try [`(\d+ *\w+)`](https://regex101.com/r/ZPxtXX/1) (this will select all letters after the number) Click on them to see a breakdown on how they work. – GrumpyCrouton Nov 05 '19 at 21:18
  • Have a play with online tools such as regex101.com eg https://regex101.com/r/jUj3IF/1 – Scuzzy Nov 05 '19 at 21:19

0 Answers0