How to count two words as 1 in same line

dsimsek

In the text file I've got, each sentence is represented with a specific type such as: contrast.

A contrasting sentence can either be represented with a tag "CONTRAST" or "CONTR" or "WEAKCONTR". For instance:

IMPSENT_CONTRAST_VIS(Studying networks in this way can help to
identify the people from whom an individual learns , where
conflicts_MD:+ in understanding_MD:+ may originate , and which
contextual factors influence learning .)

So I count these with following expression: /(\_(WEAK))|(\_CONTRAST)|(\_CONTR(\_|\())/g which works perfectly fine.

Now the problem is some sentences are expressed with more than one contrast tag such as CONTR & WEAKCONTR together. For instance:

IMPSENT_CONTRAST_EMPH_WEAKCONTR_VIS(Studying_MD:+ networks in this way can help to identify_MD:+ the people from whom an individual learns , where conflicts_MD:+ in understanding_MD:+ may originate , and which contextual factors influence learning .)

At this point I have to count these as 1 not 2. Do you have any idea how possible this is with RegExp?

Lucas Trzesniewski

You can use lookaheads to assert it, and then count the matches:

(?=\w*_(?:WEAK|CONTRAST|CONTR[_)]))\b\w+\b

Demo here: http://regex101.com/r/xP2yI7/3
Notice the match count.

This will match the whole IMPSENT_CONTRAST_EMPH_WEAKCONTR_VIS expression, but only if it matches the part in the lookahead, which filters for the keywords you're looking after. This will match even if you have multiple such sentences on the same line.

Also, I've simplified your regex a bit, retaining the same meaning. Notice you don't have to escape the _.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

count the number of the same and repeated words

From Dev

How to ensure two words stay on the same line?

From Dev

How to count same words in a string and get the index of the first word that is equal?

From Dev

how do i count occurrence of words in a line

From Dev

How to count two words as 1 in same line

From Dev

How can I count the number of equal words between two strings?

From Dev

How to extract one/two/three adjacent words from a line?

From Dev

PHP How to count same words in foreach loop

From Dev

Count recurrent words in two files

From Dev

How to concatenate two functions on the same line?

From Dev

How to use two let's on the same line?

From Dev

AWK: How to exactly match and print multiple words between two keywords in the same line

From Dev

How to sed two words in a line and add before and after?

From Dev

Sepration of words and count in columns and seperate then in two words

From Dev

How to count the same words found in two different strings using php?

From Dev

Match two words that is on the same line

From Dev

How to count the lines containing one of two words but not both

From Dev

How to print multiple words from same line in Perl?

From Dev

How can I count the number of equal words between two strings?

From Dev

Comparing two url-slugs to find count of same words

From Dev

How to return the count of the same elements in two lists?

From Dev

How to find files with same name but different line count in two directories?

From Dev

how to check if two points are on the same line in postgis

From Dev

bash + how to verify all words in each line with the same count

From Dev

How to concatenate two functions on the same line?

From Dev

How to count words in a line

From Dev

How to set two image and text in same line

From Dev

How to break these two words in two line

From Dev

How to set two block on the same line by middle?

Related Related

  1. 1

    count the number of the same and repeated words

  2. 2

    How to ensure two words stay on the same line?

  3. 3

    How to count same words in a string and get the index of the first word that is equal?

  4. 4

    how do i count occurrence of words in a line

  5. 5

    How to count two words as 1 in same line

  6. 6

    How can I count the number of equal words between two strings?

  7. 7

    How to extract one/two/three adjacent words from a line?

  8. 8

    PHP How to count same words in foreach loop

  9. 9

    Count recurrent words in two files

  10. 10

    How to concatenate two functions on the same line?

  11. 11

    How to use two let's on the same line?

  12. 12

    AWK: How to exactly match and print multiple words between two keywords in the same line

  13. 13

    How to sed two words in a line and add before and after?

  14. 14

    Sepration of words and count in columns and seperate then in two words

  15. 15

    How to count the same words found in two different strings using php?

  16. 16

    Match two words that is on the same line

  17. 17

    How to count the lines containing one of two words but not both

  18. 18

    How to print multiple words from same line in Perl?

  19. 19

    How can I count the number of equal words between two strings?

  20. 20

    Comparing two url-slugs to find count of same words

  21. 21

    How to return the count of the same elements in two lists?

  22. 22

    How to find files with same name but different line count in two directories?

  23. 23

    how to check if two points are on the same line in postgis

  24. 24

    bash + how to verify all words in each line with the same count

  25. 25

    How to concatenate two functions on the same line?

  26. 26

    How to count words in a line

  27. 27

    How to set two image and text in same line

  28. 28

    How to break these two words in two line

  29. 29

    How to set two block on the same line by middle?

HotTag

Archive