Hi i am using regex in Matlab.
I need to find number of hits for few words while ignoring other few words using regex
what i have tried so far:
String = 'Sunday:Monday:Tuesday:Wednesday:Thursday:Friday:Saturday:Sun:Mon:Tue:Wed:,Thu:,Fri:,Sat:';
Output = regexp( String,'^(?!.*(,Sun:|,Sunday:)).*(Sun:|Sunday:)' )
The Output of above regexp comes as true, But need it as 2 as it got hit 2 times for Sun: and Sunday:.
In next Scenario:
String = 'Sunday:Monday:Tuesday:Wednesday:Thursday:Friday:Saturday:Sun:Mon:Tue:Wed:,Thu:,Fri:,Sat:';
Output = regexp( String,'^(?!.*(,Fri:|,Friday:)).*(Fri:|Friday:)' )
The Output of above regexp comes as false, But need it as 1 as it*** got hit 1 time*** for Friday:.
I also tried:
regexp( String,'^(?!.*(,Sun:|,Sunday:)).*(Sun:|Sunday:)' ,'match')
But its giving Output as whole string. I am confused how to get number of hits while ignoring other words, Help would be appreciated regexp work in Matlab same as normal.
You can use
(?<!,)Fri(?:day)?:
It matches
(?<!,)
- a location not immediately preceded with ,
Fri
- Fri
(?:day)?
- an optional day
string:
- a colon.See the regex demo.
If you allow some redundancy, you may build the pattern like this:
(?<!,)(Fri:|Sunday:)
It will match Fri:
or Sunday:
not immediately preceded with a comma.