Click here to Skip to main content
15,614,943 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
The objective of this C/C++ regular expression is to extract ONLY first word in the line . It works as expected when there is a single space between words.
Such as
"Menu space main " results in "Menu"
.

It fails when there are multiple spaces between words.
" advertise                             Advertise Options Submenu"


gives

advertise
Advertise
Options
Submenu

only single "advertise" is desired




Menu main: \n Available commands: \n ------------------- \n advertise Advertise Options Submenu\n monitor Advertisement Monitor Options Submenu\n scan Scan Options Submenu\n gatt Generic Attribute Submenu\n admin Admin Policy Submenu\n list List available controllers\n show ctrl Controller information\n select <ctrl> Select default controller\n devices List available devices\n paired-devices List paired devices\n system-alias <name> Set controller alias\n reset-alias Reset controller alias\n power <on off=""> Set controller power\n pairable <on off=""> Set controller pairable mode\n discoverable <on off=""> Set controller discoverable mode\n discoverable-timeout value Set discoverable timeout\n agent <on off="" capability=""> Enable/disable agent with given capability\n default-agent Set agent as the default one\n advertise <on off="" type=""> Enable/disable advertising with given type\n set-alias <alias> Set device alias\n scan <on off="" bredr="" le=""> Scan for devices\n info dev Device information\n pair dev Pair with device\n cancel-pairing dev Cancel pairing with device\n trust dev Trust device\n untrust dev Untrust device\n block dev Block device\n unblock dev Unblock device\n remove <dev> Remove device\n connect <dev> Connect device\n disconnect dev Disconnect device\n menu <name> Select submenu\n version Display version\n quit Quit program\n exit Quit program\n help Display help about this program\n export Print environment variables\n "

What I have tried:

I changed the regular expression to "(\\w+)\\s+" - no change.
Then I tried "(\\w+)\\W" and it made things worse - now extracting ALL words.
Posted
Updated 21-Aug-22 5:37am
v3
Comments
Member 14968771 21-Aug-22 8:14am    
When answering a question please....

how does reformatting the original post ANSWERS the question? Please do not waste yours and mine time on superfluous beautification - such post is on borderline of "correcting English "...

You failed in second part "EDIT AND FIX " you did not fix anything
Patrice T 21-Aug-22 11:34am    
The only change I made only made obvious the "multiple spaces" part.
As for the "main menu" part, I didn't change any thing as I don't know what it is supposed to be.
Richard MacCutchan 21-Aug-22 12:07pm    
When posting questions, please ensure that code and text blocks are formatted correctly, for readability. Which is all that Partice did.

1 solution

Quote:
I changed the regular expression to "(\\w+)\\s+" - no change.
Then I tried "(\\w+)\\W" and it made things worse - now extracting ALL words.

To extract all words, no matter the spaces, commas or points, I use this RegEx
(\w+)

Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx: Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx: Regexper[^]
 
Share this answer
 
Comments
Member 14968771 24-Aug-22 20:07pm    
The objective of this C/C++ regular expression is to extract ONLY first word in the line . It works as expected when there is a single space between words.
Such as
Copy Code

"Menu space main " results in "Menu"

.

It fails when there are multiple spaces between words.
Copy Code

" advertise Advertise Options Submenu"



gives

advertise
Advertise
Options
Submenu

only single "advertise" is desired
Patrice T 24-Aug-22 20:36pm    
If you want only first word, you have to specify it in RegEx
^(\w+)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900